Simple do while loop wont work

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.
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
50
51
52
53
54
55
56

/*/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>
using namespace 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.

You need something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

	int i;
 
        cout << "Hi ! ... bla bla"
        cin >> i;

        while(i < 1 || i > 5){
               cout << ""Error msg";
               cin >> i;
        }

      switch (i){
             //stuff
      }

 

Last edited on
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 !
Topic archived. No new replies allowed.