I've always struggled with getting local variables from selection structures. I'm new to programming in general, and I need help with calculating the total purchase of items where their values are inside if functions, and I'm having trouble with my while function, because I intend to use the it to infinitely accept meal code and quantity until the user enters 0, but it doesn't work. How do I make my infinite while function work, and how do I properly get the values from selection structures? Any help, tips, and suggestions are appreciated!
Update: I was able to fix my problem with variables inside functions. Current problem is that I need to type in 0 twice because the first 0 gets ignored, and I don't know why.
int main()
{
char mealCode;
bool noOrder = false;
int quantity = -1, subTotalA = 0, subTotalB = 0, subTotalC = 0, subTotalD = 0, subTotalE = 0, total = 0;
while (quantity != 0)
{
cin >> mealCode >> quantity;
if (quantity == 0)
break;
else
{
if (mealCode == 'A')
{
subTotalA += 45 * quantity;
cout << "Subtotal is " << subTotalA << endl;
}
else if (mealCode == 'B')
{
subTotalB += 50 * quantity;
cout << "Subtotal is " << subTotalB << endl;
}
else if (mealCode == 'C')
{
subTotalC += 55 * quantity;
cout << "Subtotal is " << subTotalC << endl;
}
else if (mealCode == 'D')
{
subTotalD += 60 * quantity;
cout << "Subtotal is " << subTotalD << endl;
}
else if (mealCode == 'E')
{
subTotalE += 75 * quantity;
cout << "Subtotal is " << subTotalE << endl;
}
}
total = subTotalA + subTotalB + subTotalC + subTotalD + subTotalE;
cout << "Total Purchase is" << total;
}
}