Reputation: 81
Hi all I have been having some problems with a task I have been set
The first part of the task was to output a price table with the following rules:
The price for up to 50 is £5 each. For between 51 and 80, the price is £4 each, while for between 81 and 100 the price is a rock bottom £2.50 each. Using loop structures and selection statements (if.. etc) your program should output a widget price chart for widgets in multiples of 10 up to 100.
I have done this however the second part of the task has me stumped after the table has been output to input a number of widgets. You should then calculate the cost and output the value. If the user enters ‘q’ or ‘Q’, the program should terminate.
Here is the full code
using System;
namespace w5Task3
{
class Program
{ public static void Main ( string[] args )
{
double PriceEach1 = 5;
double PriceEach2 = 4;
double PriceEach3 = 2.50;
double Quantity = 10;
int UserOrder=0;
Console.WriteLine("\n\nBelow is the price chart:\n\n");
Console.WriteLine("WidgetQuantity\t\t\tPrice\n");
while (Quantity <=100)
{
double Price1 = PriceEach1*Quantity;
double Price2 = PriceEach2*Quantity;
double Price3 = PriceEach3*Quantity;
if (Quantity <=50)
{
Console.WriteLine("\t{0}\t\t\t{1:C}", Quantity, Price1);
}
if(Quantity >=51 && Quantity <=80)
{
Console.WriteLine("\t{0}\t\t\t{1:C}", Quantity, Price2);
}
if (Quantity >80 && Quantity <=100)
{
Console.WriteLine ("\t{0}\t\t\t{1:C}",Quantity, Price3);
}
Quantity +=10;
}
while (UserOrder >=0)
{
try
{
Console.WriteLine("Enter the amount of widgets you would like to purchase or press q to quit");
string temp = Console.ReadLine();
if (temp =="q") break;
if (temp =="Q") break;
int.TryParse(temp, out UserOrder);
double UserPrice;
if (UserOrder <=50)
{
UserPrice = UserOrder*5;
Console.WriteLine("The price is {0:C}",UserPrice);
}
if (UserOrder >=51 && UserOrder <=80)
{
UserPrice = UserOrder*4;
Console.WriteLine("The price is {0:C}",UserPrice");
}
if (UserOrder >80)
{
UserPrice = UserOrder*2.5;
Console.WriteLine("The price is {0:C}",UserPrice");
}
}
catch(Exception)
{
Console.WriteLine("You have entered an incorrect value. Please enter a number or press q to quit");
}
}
}
}
The part I am having issues with is:
while (UserOrder >=0)
{
try
{
Console.WriteLine("Enter the amount of widgets you would like to purchase or press q to quit");
string temp = Console.ReadLine();
if (temp =="q") break;
if (temp =="Q") break;
int.TryParse(temp, out UserOrder);
double UserPrice;
if (UserOrder <=50)
{
UserPrice = UserOrder*5;
Console.WriteLine("The price is {0:C}",UserPrice);
}
if (UserOrder >=51 && UserOrder <=80)
{
UserPrice = UserOrder*4;
Console.WriteLine("The price is {0:C}",UserPrice");
}
if (UserOrder >80)
{
UserPrice = UserOrder*2.5;
Console.WriteLine("The price is {0:C}",UserPrice");
}
}
catch(Exception)
{
Console.WriteLine("You have entered an incorrect value. Please enter a number or press q to quit");
}
}
}
I can get the program to quit or do one UserPrice but it is when I need to make it change the price dependent on the amount ordered.
Any help or suggestions are much appreciated!
Upvotes: 3
Views: 1441
Reputation: 7619
I think your problem is the quote after UserPrice on rows 64 and 70..
Console.WriteLine("The price is {0:C}",UserPrice");
remove it
Upvotes: 2
Reputation: 21147
I'm having trouble understanding what exactly the problem you're having is. Are you struggling to get the right calculation for the order or is the Quit behavior not acting in the way that you're expecting.
In general I think its bad form to use a transient variable like that for your while loop condition. When I want a console app to repeat I typically set up a variable specifically used for the loop condition like this:
bool isRunning = true;
while (isRunning)
{
....
Then when you check for your input
if (temp.ToUpper() == "Q")
{
isRunning = false;
break;
}
Upvotes: 0
Reputation: 3221
You have newlines in your constant expressions at lines 67 and 73!
Fix those, and it all runs fine.
Upvotes: 2
Reputation: 13696
The easiest way to do it is to adjust UserOrder
after you calculate the price for that portion of the order. You can go in either direction, but it basically looks like:
if (HasThisQuantityRange)
{
Total += ThisAmount * (Lower of: ThisQuantity or UserOrder);
UserOrder -= (Lower of: ThisQuantity or UserOrder);
}
if (UserOrder > 0 && HasAdjustedQuantityRange)
{
Total += ThisAmount * (Lower of: ThisQuantity or UserOrder);
UserOrder -= (Lower of: ThisQuantity or UserOrder);
}
and so on.
Obviously, this is all pseudocode, and you'll need to implement it yourself, but this will hopefully get you pointed in the right direction.
Upvotes: 1