Reputation: 1
Sorry, I'm having a bit of trouple with this, I've missed out on most of the array lectures so I had to rely on Google to catch up as I need to implement them into this assignment.
So I have two arrays, one to display the name(toppings of a pizza) and another to assign the value of each topping to(can only have a maximum of three per topping, like a single, double or triple topping of cheese). There's probably a way to keep this as one array but I wasn't sure of a way to give a string a value, unless casting works.. I've only done that like once so I'm not sure about it.
Anyways, instead of listing out all 9 toppings, I wanted to make a simple statement that would cater for all of them and replaced the array value with an integer. I don't have it on this computer but it was something like:
int i = toppSelection - 1; //If users presses 1 first topping is selected, but first
//topping in array would be 0
if(toppSelection > 0 && toppSelection <10)
{
toppAmount[i] = toppAmount[i]++
}
Not including the error statements.
I put it all in a while loop so that each time it increments, the toppings name and amount arrays are written out again so it refreshes but the values won't increase. They just stay the same.
Sorry if this is horribly obvious but I'm losing my patience with this now and I'd rather find out how to do it right instead of making a ton of statements for each value of the array.
Thanks in advance
Upvotes: 0
Views: 519
Reputation: 1
static void ToppingMenu()
{
int toppSelection = 0;
while (toppSelection != 999)
{
Console.Clear();
Console.WriteLine("Create Pizza Menu");
string[] toppName = new string[10] { "Cheese ", "Tomato ", "Mushrooms ", "Green Pepper ", "Black Olives ", "Onions ", "Pepperoni ", "Chicken ", "Tuna ", "End Custom Pizza Creation - Previous Menu" };
int[] toppAmount = new int[9] { 1, 1, 0, 0, 0, 0, 0, 0, 0 };
Console.WriteLine("{0} {1}", toppName[0], toppAmount[0]); //Cheese
Console.WriteLine("{0} {1}", toppName[1], toppAmount[1]); //Tomato
Console.WriteLine("{0} {1}", toppName[2], toppAmount[2]); //Mushrooms
Console.WriteLine("{0} {1}", toppName[3], toppAmount[3]); //Green Pepper
Console.WriteLine("{0} {1}", toppName[4], toppAmount[4]); //Black Olives
Console.WriteLine("{0} {1}", toppName[5], toppAmount[5]); //Onions
Console.WriteLine("{0} {1}", toppName[6], toppAmount[6]); //Pepperonni
Console.WriteLine("{0} {1}", toppName[7], toppAmount[7]); //Chicken
Console.WriteLine("{0} {1}", toppName[8], toppAmount[8]); //Tuna
Console.WriteLine("\n\n\n{0} ", toppName[9]); //Exit to previous menu option
Console.WriteLine("\n\nTo finish order, please enter '999'");
Console.Write("\n\nSelection: ");
toppSelection = int.Parse(Console.ReadLine());
int i = toppSelection - 1; //i is assigned same value as number entered -1,
//i-1 fixes the 1-off fault, where 0 = first topping in array but 1 = first topping by user entry
if (toppAmount[i] > 2) //
{
Console.Write("Error, invalid amount");
}
else if (toppSelection > 0 && toppSelection < 10)
{
toppAmount[i]++;
}
Got all of my other methods done now so I have come back to this. I'm expecting I have made an embarrassingly basic mistake(or many!) but here it is, sorry the late hour. Some of my coding probably seems a bit primitive but this is what we'd be expected to do from what we've been taught this year. No sense in replacing things with something I haven't covered yet so if just worry about the logic/syntax of the values I want to increment =]
toppName = Array of named toppings toppAmount = Array of topping values, two start at one because they're default choices
toppSelection = User input and defines which topping is selected
Thanks again for the help guys, sorry to trouble you.
Upvotes: 0
Reputation: 174329
Just use toppAmount[i]++;
instead of toppAmount[i] = toppAmount[i]++;
. For an explanation, see Marc's answer or here: what value will have property of my object?
Upvotes: 4
Reputation: 1062955
Let's consider the line:
toppAmount[i] = toppAmount[i]++;
this:
toppAmount[i]
, and since it is post increment the "value" here is the old valuetoppAmount[i]
toppAmount[i]
, undoing all your workYou just want:
toppAmount[i]++;
Upvotes: 1