Looping in this situation

Hello, I am doing some beginner exercises and have been looking around for the internet for an answer to this question, but haven't found one that's entirely applicable.
What I'm looking for the program to do is return to the initial question if the user types in a variable less than 1 or greater than 5.
Sorry, I realize this may be a very n00b question, but I assure you I have looked around for an answer.


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
57
  #include <iostream>
#include <string>
using namespace std;

int main()
{
	
		
	

	cout << "Aight here's what we got\n1. Coca Cola\n2. Pepsi\n3. Sprite\n4. Watermellon\n5. Lean\n" << endl;
	cout << "So which one you want dude?" << endl;
	const string choices = "Aight let me get you that";
	int options;
	
	{

		cin >> options;

		if (options == 1)
		{
			cout << choices + " Coca Cola" << endl;

		}
		if (options == 2)
		{
			cout << choices + " Pepsi" << endl;

		}
		if (options == 3)
		{
			cout << choices + " Sprite" << endl;
		}
		if (options == 4)
		{
			cout << choices + " Watermellon" << endl;

		}
		if (options == 5)
		{
			cout << choices + " lean" << endl;

			
		}
		if (options > 1 || options < 6)
		{
			cout << options << + " isn't an option try again" << endl;
		}
		
	}
		
		system("pause");
		return 0;
	

}
I'd probably use a while loop to validate the input.

There's info on the various loops on this page here.
http://www.cplusplus.com/doc/tutorial/control/

Line 45 - Double check the logic here
Thank you @wildblue
I attempted to use a while loop but I guess I didn't fully understand the concept. Will try again.
If you're still struggling with using while loops. Basically it's just a condition that allows a section of your code to repeat until that condition changes in some way.

For instance,

1
2
3
4
5
bool quit = false; 

while(!quit) {
    cout << "bob";
}


The while loop above will ONLY loop that statement whilst "quit" is false. The loop will end once "quit" becomes true. The above code is actually an infinite loop because "quit" never becomes true as you can see.

The code below however, you can clearly see that I change the boolean variable to true after "bob" has been printed to the console. That while loop would only execute once.

1
2
3
4
5
6
bool quit = false;

while(!quit) {
    cout << "bob";
    quit = true;
}


This is your code with a while loop, try comparing and contrasting your old code with this one.

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
#include <iostream>
#include <string>
using namespace std;

int main() {

	bool quit = false; // The while loop condition

	cout << "Aight here's what we got\n1. Coca Cola\n2. Pepsi\n3. Sprite\n4. Watermelon\n5. Lean\n6. Quit\n" << endl;

	while (!quit) { // This while loop will loop whilst quit is false

		cout << "So which one you want dude: ";
		const string choices = "Aight let me get you that";
		int options;

			cin >> options;

			if (options == 1) {
				cout << choices + " Coca Cola\n" << endl;

			}
			if (options == 2) {
				cout << choices + " Pepsi\n" << endl;

			}
			if (options == 3) {
				cout << choices + " Sprite\n" << endl;
			}
			if (options == 4) {
				cout << choices + " Watermellon\n" << endl;

			}
			if (options == 5) {
				cout << choices + " lean\n" << endl;
			}
			if (options == 6){
				cout << "You quit.\n" << endl;
				quit = true; // This ends the while loop
			}
			if (options < 1 || options > 7) { // Compare logic with your old code
				cout << options << +" isn't an option, try again.\n" << endl;
			}

		}
	return 0;
}


Best of luck dude. Also as wildblue just pointed out, your logic isn't correct either.
Last edited on
Try making a while loop with a condition that can only be met by inputting a valid value within the loop.

I would recommend something like this:
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
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
using namespace std;

int main() {

int invalid = 1;

while (invalid == 1)
{

	cout << "Aight here's what we got\n1. Coca Cola\n2. Pepsi\n3. Sprite\n4. Watermelon\n5. Lean\n6. Quit\n" << endl;

		cout << "So which one you want dude: ";
		const string choices = "Aight let me get you that";
		int options;

cin >> options;

		if (options == 1)
		{
			cout << choices + " Coca Cola" << endl;
        invalid = 0;

		}
		else if (options == 2)
		{
			cout << choices + " Pepsi" << endl;
        invalid = 0;

		}
		else if (options == 3)
		{
			cout << choices + " Sprite" << endl;
        invalid = 0;
		}
		else if (options == 4)
		{
			cout << choices + " Watermellon" << endl;
        invalid = 0;

		}
		else if (options == 5)
		{
			cout << choices + " lean" << endl;
        invalid = 0;

			
		}
		else if (options == 6)
		{
             invalid = 0;
  
            
                  
                }
		else
		{ 
            system("cls");
			cout << options << + " isn't an option try again" << endl;
                      invalid = 1;
		}
        
}//end while
system("pause");
}

Last edited on
^^ Repeating redundant code is almost always bad practice.

I wouldn't recommend that way but that's just me. I THINK he wants the loop to be around asking the user again for input.
Last edited on
Sorry, I'm a super noob :) there is an cleaner way to do it but I thought that his program was just for fun and it wouldn't really matter. My way is just a simple fix for what you are trying to do. If your program needs to look nice or is an assignment, then sausage's way is what you are looking for. Sausage's method will develop better coding habits too and is overall a better method.

here is more info on both methods:

sausage's way (booleans):
http://www.learncpp.com/cpp-tutorial/26-boolean-values/

My way (while loop):

http://www.cprogramming.com/tutorial/lesson3.html

We're all learning. But you're correct, there's more than one way to kill a fish, it just depends on what you're trying to achieve.

1
2
3
4
}//end while
system("pause");
return 0; // *cough* return statement *cough* ;D
}
Last edited on
1
2
3
4
}//end while
system("pause");
return 0; // *cough* return statement *cough* ;D
}


Thanks for the catch! Can't believe I didn't realize that XD It's nice to see a friendly community of programmers of all skill levels helping each other out.
if (options < 1 || options > 7) { // Compare logic with your old code


> 6 ? 7 doesn't seem to be a valid option from what was posted.
Topic archived. No new replies allowed.