Tracking User input and Case number user input

I am currently new to c++ and I was wondering what loop am I supposed to use in order to keep track on the total money amount of the user input and the
total orders of the user input from switch in order to print the receipt
after deciding not to order again: (My code is quite long so I only put the case 1 since it's pretty much the same for cases 2, 3, and 4.)



#include <iostream>
using namespace std;

int main(){
float x;
int y;
cout<<"Enter your selected amount of money: $";
cin>>x;

menu:cout<<"Welcome to Sunny's' Breakfast shop:\n1.Burger\t\t$6.75\n2.Sausage\t\t$7.94\n3.Pancake\t\t$5.34\n4.Cheese Sandwich\t$3.69\n"<<endl;
cout<<"Pick your order: ";
cin>>y;

switch(y){
case 1:
c1:if(x<6.75){
int temp;
while(x<6.75){
cout<<"Insufficient funds! You lack $"<<(x-6.75)*-1<<" Enter additional amount: $";
cin>>temp;
x+=temp;
goto c1;
}
}

else{
string z;
x-=6.75;
cout<<"Thank you for ordering, your change is: $"<<x<<endl<<endl;
b1:cout<<"Order again? Y or N: ";
cin>>z;
if(z=="Y"||z=="y"){
cout<<endl<<endl;
goto menu;
}

else if(z=="N"||z=="n"){
cout<<"Thank you for buying, Have a nice day!"; //-----> This is where I think I need to put that condition where I can track money input and order input and print it here.
}

else{
cout<<"Invalid keyword...\n";
goto b1;
}
}
break;
Last edited on
Pretty much any loop at all is better than using goto for this. An actual loop. for, or while.

Stop using goto. It's terrible. Whatever good use cases exist for it, it's not this.

Create a variable to store the total amount BEFORE the loop.
Inside the loop, each time the user orders something, add the amount to that total.

After the loop, when the user has finished ordering, you then have that total amount.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;

int main() {
	float x;
	int y;
	cout << "Enter your selected amount of money: $";
	cin >> x;

menu:
	cout << "Welcome to Sunny's' Breakfast shop:\n1.Burger\t\t$6.75\n2.Sausage\t\t$7.94\n3.Pancake\t\t$5.34\n4.Cheese Sandwich\t$3.69\n" << endl;
	cout << "Pick your order: ";
	cin >> y;

	switch (y) {
		case 1:
		c1:
			if (x < 6.75) {
				int temp;

				while (x < 6.75) {
					cout << "Insufficient funds! You lack $" << (x - 6.75) * -1 << " Enter additional amount: $";
					cin >> temp;
					x += temp;
					goto c1;
				}
			} else {
				string z;

				x -= 6.75;
				cout << "Thank you for ordering, your change is: $" << x << endl << endl;
			b1:
				cout << "Order again? Y or N: ";
				cin >> z;

				if (z == "Y" || z == "y") {
					cout << endl << endl;
					goto menu;
				} else
					if (z == "N" || z == "n") {
						cout << "Thank you for buying, Have a nice day!"; //-----> This is where I think I need to put that condition where I can track money input and order input and print it here.
					} else {
						cout << "Invalid keyword...\n";
						goto b1;
					}
			}
			break;
	}
}


OMG. That takes me back to when I started to code in Dartmouth Basic /HP Basic back in the 1970's.

DON'T use goto. Who taught you that? Now that you know about it, erase it from your memory permanently. Repeat. goto does not exist. goto does not exist. goto does not exist........

As Repeater mentions, use a loop instead.
I just searched randomly on google on how to go back on a menu then I found that lol thanks... So I'm removing the goto but if I'm going back to the menu after the first transaction is done can I call the switch? Or put the switch inside a function so that I can go back to menu?
Last edited on
A common pattern for menus is to have a endless loop inside main:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  for (;;)
  {
    display_menu();
    auto choice = get_choice();
    switch(choice)
    {
      case exit_condition: 
        break;
    }
  }
}
I can't grasp the meaning of the keyword "auto" and is "display menu()" a function? I can't get the logic if I use for loops I was thinking using functions instead is that legal? Use of functions instead of for loops?
Last edited on
keyword auto is quite simple. When defining and initialising a variable, auto means that the type of the variable is the type of the value with which it is initialised.

 
auto c {myfunc()};


type of variable c is the return type of myfunc()

In the code above, the type of variable choice is the return type of get_choice(). The compiler always knows the type of the initialisation value.

Yes, display_menu() is a function.
Last edited on
I don't get it sorry I'll get to that soon what I did is a do while loop however the condition I put on the while doesn't work properly even though it compiled successfully the syntax is like this

do{
switch()
case1:
if{}
elseif{
You want to order again?
cin>>z
}
break;
default:
break;

}while(z!="n"||"N");

The code runs well if the syntax doesn't include the uppercase letter. So my question why is the loop not stopping if the uppercase was typed.
Last edited on
 
while(z!="n"||"N");


The conditional test is not correct. This needs to be written as :

 
while(z != "n" && z != "N");

Last edited on
Topic archived. No new replies allowed.