Write a program that prompts the user to make a choice
for a pizza size—S, M, L, or X—and then displays the
price as $6.99, $8.99, $12.50, or $15.00, respectively. Save
the program as PizzaPrices.cs.
b. Modify the PizzaPrices program so that the following
discounts apply: no discount for one pizza, 10% for two
pizzas, 15% for three or four pizzas, and 20% for five or
more pizzas. Display a full accounting of the transaction
So I am just stuck... Do I have to use array here? cause it seems its a lot easier to use "if" statement. But if there's a way to solve this problems using arrays, could you show me how plz ?
string[] pizzaSize = { "S", "M", "L", "G" };
double[] pizzaPrice = { 6.99, 8.99, 12.50, 15.00 };
string validPizzasiza;
Console.WriteLine(" Enter pizza size: ");
string userInput = Console.ReadLine();
for (int x = 0; x < pizzaSize.Length; ++x)
{
if (validPizzasiza == pizzaSize[x])
{
}
Loading
VulpesPosted Jan 8, 2012, 3:49 PM
Try this:
Posted Jan 9, 2012, 10:52 AM
It is a greatly valuable explanation because I had many confusion over using the FOR loop, breaking loop with i = size.Length; and using the Reversed Keyword BREAK. Your explanation solved all the problems.
VulpesPosted Jan 9, 2012, 10:40 AM
i = size.Length;
which will be outside the range of the price array.
However, if you'd replaced the above line with:
break; // the usual way to exit loops
then it would have worked.
Posted Jan 9, 2012, 10:32 AM
//for (i = 0; i < size.Length; ++i)
tPriceBeforeDis = price[i] * num;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x = 4, i;
double tPriceBeforeDis = 0, tPriceAfterDis = 0, pSize = 0;
string[] size = new string[4] { "S", "M", "L", "X" }; //i=3
double[] price = new double[4] { 6.99, 8.99, 12.50, 15 }; //i=3
int[] numberRangeLmit = new int[5] { 1, 2, 3, 4, 5 }; //x=4
double[] discount = new double[5] { 0, 0.1, 0.15, 0.15, 0.20 }; //x=4
Console.WriteLine("Pizza size - S M L X");
Console.Write("Which size you want ");
string s = Console.ReadLine();
for (i = 0; i < size.Length; ++i)
{
if (s == size[i])
{
Console.WriteLine("Price of a Pizza is {0}", price[i].ToString("C"));
//pSize = price[i]; //(1) comment out
i = size.Length;
}
}
Console.Write("\nHow many Pizza you want ");
int num = Convert.ToInt32(Console.ReadLine());
//for (i = 0; i < size.Length; ++i)
tPriceBeforeDis = price[i] * num; //?????????????????????????
//tPriceBeforeDis = pSize * num; //(2) comment out
Console.WriteLine("\nTotal price before discount is {0}", tPriceBeforeDis.ToString("C"));
while (x >= 0 && num < numberRangeLmit[x])
--x;
tPriceAfterDis = tPriceBeforeDis - (tPriceBeforeDis * discount[x]);
Console.WriteLine("Price after discount ({0}% discount) {1}", discount[x], tPriceAfterDis.ToString("C"));
Console.ReadKey();
}
}
}
VulpesPosted Jan 9, 2012, 10:01 AM
for (i = 0; i < size.Length; ++i)
tPriceBeforeDis = price[i] * num;
tPriceBeforeDIS will be successively overwritten until it's eventually set to the price for the largest size of pizza (i.e. $15 each) multiplied by the number of pizzas required.
Previously, when you had this:
pSize = price[i];
// and later
tPriceBeforeDis = pSize * num;
pSize was being set to the price for the size of pizza which was actually being ordered and this was then being multiplied by the number of pizzas required to arrive at tPriceBeforeDis.
Posted Jan 8, 2012, 8:49 PM
When I comment out step (1) and (2) (highlighted in yellow) instead I inserted above code in (highlighted in blue) program is giving incorrect output. Anyone knows the reason?
Following is the incorrect output
/*
Pizza size - S M L X
Which size you want S
Price of a Pizza is $6.99
How many Pizza you want 1
Total price before discount is $15.00
Price after discount (0% discount) $15.00
*/
Posted Jan 8, 2012, 7:51 PM
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x = 4, i;
double tPriceBeforeDis = 0, tPriceAfterDis = 0, pSize=0;
string[] size = new string[4] {"S","M","L","X"}; //i=3
double[] price = new double[4] {6.99, 8.99, 12.50, 15}; //i=3
int[] numberRangeLmit = new int[5] {1, 2, 3, 4, 5}; //x=4
double[] discount = new double[5]{0, 0.1, 0.15, 0.15, 0.20}; //x=4
Console.WriteLine("Pizza size - S M L X");
Console.Write("Which size you want ");
string s = Console.ReadLine();
for (i = 0; i < size.Length; ++i)
{
if (s == size[i])
{
Console.WriteLine("Price of a Pizza is {0}", price[i].ToString("C"));
pSize = price[i];
i = size.Length;
}
}
Console.Write("\nHow many Pizza you want ");
int num = Convert.ToInt32(Console.ReadLine());
tPriceBeforeDis = pSize * num;
Console.WriteLine("\nTotal price before discount is {0}", tPriceBeforeDis.ToString("C"));
while (x >= 0 && num < numberRangeLmit[x])
--x;
tPriceAfterDis = tPriceBeforeDis - (tPriceBeforeDis * discount[x]);
Console.WriteLine("Price after discount ({0}% discount) {1}", discount[x], tPriceAfterDis.ToString("C"));
Console.ReadKey();
}
}
}
Prime bPosted Jan 8, 2012, 3:12 PM
string[] pizzaSize = { "S", "M", "L", "G" }
double[] pizzaPrice = { 6.99, 8.99, 12.50, 15.00 };
while (true)
{
Console.Write("Enter pizza size: ");
string userInput = Console.ReadLine().ToUpper();
Console.WriteLine("Please enter how many pizzas you need: ");
double userInput1 = Convert.ToDouble(Console.ReadLine());
int foundIndex =-1;
double firstDiscount = 0, secondDiscound = 0, thirdDiscount = 0;
//if( userInput1 == 2)
// firstDiscount = userInput1 * .10;
//Console.WriteLine("Due to discount of 10% your total price is {0}", firstDiscount);
//if (userInput1 == 3 || userInput1 == 4)
// secondDiscound = userInput1 * .15;
//Console.WriteLine("Due to discount of 10% your total price is {0}", secondDiscound);
//if (userInput1 >= 5)
// thirdDiscount = userInput1 * .20;
//Console.WriteLine("Due to discount of 10% your total price is {0}", thirdDiscount);
for (int x = 0; x < pizzaSize.Length; ++x)
{
if (userInput == pizzaSize[x])
{
foundIndex = x;
}
}
if (foundIndex > -1)
{
if (userInput1 == 2)
firstDiscount =pizzaPrice[x] * .10;
Console.WriteLine("Due to discount of 10% your total price is {0}", firstDiscount);
if (userInput1 == 3 || userInput1 == 4)
secondDiscound = * .15;
Console.WriteLine("Due to discount of 15% your total price is {0}", secondDiscound);
if (userInput1 >= 5)
thirdDiscount = * .20;
Console.WriteLine("Due to discount of 20% your total price is {0}", thirdDiscount);
Console.WriteLine("The price of a size {0} pizza is {1:C}", userInput, pizzaPrice[foundIndex]);
return;
}
else
{
Console.WriteLine("Invalid size, please try again");
}
VulpesPosted Jan 8, 2012, 3:06 PM
The usual choice for this type of 'find' routine is -1.
Compare, for example, the String.IndexOf method in the .NET framework:
http://msdn.microsoft.com/en-us/library/kwb0bwyd.aspx
Prime bPosted Jan 8, 2012, 2:49 PM
VulpesPosted Jan 8, 2012, 2:38 PM