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.
}
#include <iostream>
usingnamespace 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;
} elseif (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........
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?
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?
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.
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.