
Kathryn C. answered 07/23/21
4+ years in tutoring and teaching with focus on memory management
Hi Malik!
There are a few things I want to break down for you that will not only help you find your error but will also help you improve and build upon your code for later. This might be long, but I hope it's especially helpful for you!
Scope:
Scope is the area of the program where an item (be it variable, constant, function, etc.) that has an identifier name is recognized. -Rebus Press.
At the start of your program below "using namespace std;" you have declared 4 functions, "Go", "Menu", "Bill", and "Payment" as well as 8 constants using the identifier "const", and 2 variables subtotal and total within the global scope.
What is a global scope?
Global scope refers to every function (including main) has access to items belonging to the global scope. Functions will not need passed parameters or returns to manipulate or access this data.
Generally speaking, declaring variables globally is bad practice. This is because oftentimes you want full control over a variable and do not want unexpected or unprecedented changes. Instead, you should be using local variables. Local variables are the variables you declare within a function (such as main) because they refer to the local scope. An easy way to remember scope is looking at the curly brackets " { }" anything within them is within a localized scope while everything outside of brackets is globalized.
Functions:
Functions are great ways of breaking up code into smaller bits and or allowing the same set of code to run multiple times. Oftentimes these functions need access to out-of-scope variables or to return information to the scope in which they were called. Using parameters allows you to access out-of-scope variables and using returns allows you to give information back.
Parameters:
void functionName( dataTypeOfParameter parameter1, dataTypeOfParameter2 parameter2);
example:
void sumOfTwoNumbers( int num1, int num2);
Returns:
returnType functionName( type param, type param);
example:
int sumOfTwoNumbers( int num1, int num2) { return num1+num2; } // Provides the sum of the given numbers.
With functions and scope in mind, I recommend moving towards utilizing functions by passing and returning subtotal/total.
Input & Output:
Within your Menu function, you have error checking (which is great) but it doesn't actually error check. I believe you intended for your if statement to be a while loop. As it is now, the first incorrect input will be given an "invalid choice" error message while the second in a row would be accepted. If statements are not loops.
Your Error:
I want you to look at your "subtotal" variable and write a comment within your code on its purpose in your program. What is its fundamental difference to "total". I also want you to take a close look at your "Go" function. You're doing something similar to the below code block.
The output for this is: 20
You are overriding your "subtotal" variable. When you output it for "Food Cost $" it is only how much money water costs if you had at least 1 water.
Logic Issues:
Lastly, there are parts where you can clean up your logic. Often a trick I use with students is "how would you teach a small child to solve this problem without code" Presented the problem with "I want to buy 0 Apples (@0.12$), 2 Bannanas (@1.00$), and 7 Grapes (@0.10$)" you would not tell a child that they necessarily need to check for if there are more than 0 apples.
Cost of Apples: 0 * 0.12$ = 0
Cost of Bananas: 2 * 1.00$ = 2.00$
Cost of Grapes: 7 * 0.10$ = 0.70$
Total = cost of apples + cost of bananas + cost of grapes.
The case in which "numITEM" is 0 does not affect your program at all and added unnecessary checks gives unnecessary processing which makes your program inefficient. Learning in the future about processing time, calculations are always constant time and thus won't be a cause for slowing down your program, BUT checks, loops (especially), etc can.

Kathryn C.
Hi Malik, be sure to understand the loop in your program. I recommend. You run in a loop the totaling. Your program is like this: If I order a Gyro, the gyro count increases, it is greater than 0 so add it's price to the total. I order a water, *the gyro count is still greater than 0 so add the gyro price ONCE AGAIN to the total* water count is now one, and is greater than 0 thus, add to the total. Using output statements or cerr statements can really help you. I also recommend running through your code logic step by step with a pen and paper. You will see you add to your total every time you select an option from your menu, even if you already added it.07/24/21
Malik A.
thanks a lot, I looked again as you said and solved the "total"07/25/21
Malik A.
thanks a lot for what you wrote, it helps me so much, and I solved the subtotal because of you, but the total is still wrong when I enter 8??07/24/21