Im a beginner trying to learn c++ on my own. The program is a soda dispenser that gives you whatever soda you would want it to give you. The program works fine except the do-while loop. it simply wont loop if the input is invalid. I've googled som examples to compare with my piece of code, but i can't spot the error. As you can see the task is pretty simple, so for anyone thats slightly experienced in c++ would probably spot the error rather quickly i guess.
/*/variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
Write a program that presents the user w/ a choice of your 5 favorite beverages (Coke, Water, Sprite, ... , Whatever).
Then allow the user to choose a beverage by entering a number 1-5.
Output which beverage they chose.
If you program uses if statements instead of a switch statement, modify it to use a switch statement.
If instead your program uses a switch statement, modify it to use if/else-if statements.
Modify the program so that if the user enters a choice other than 1-5 then it will output "Error. choice was not valid, here is your money back."
/*/
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int i=0;
do{
cout<<"Hi ! welcome to the soda dispenser ! Input a number to get your soda of choice!\n""press 1 for Cola\n""press 2 for Fanta\n""press 3 for Sprite\n""press 4 for Mountain Dew\n""press 5 for Pepsi"<< endl;
cin >> i ;
}while (i<1 && i>5);
{
cout<<"Invalid number, please try again"<< endl;
}
switch (i){
case 1 : cout<<"The soda dispenser gave you a Cola!"<< endl;
break;
case 2 : cout<<"The soda dispenser gave you a Fanta!"<< endl;
break;
case 3 : cout<<"The soda dispenser gave you a Sprite!"<< endl;
break;
case 4 : cout<<"The soda dispenser gave you a Mountain Dew!"<< endl;
break;
case 5 : cout<<"The soda dispenser gave you a Pepsi!"<< endl;
break;
}
}
A number cannot be less than 1 AND greater than 5.
Another problem I see though, is the "invalid number, try again" is not going to loop back to let the user try again.
Ahh ! thanks... it worked great after some edits. I discovered my program wouldn't update the new code i wrote and i had to copy and pasta all the code in a new source file. I guess the program is another thing that takes time to learn.. Again, thanks !