Loop

3 options.

Let them be 1, 2, 3. And let 2 be the right answer.

I need the program to loop back to the question if the user enter either 1 or 3.

I have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int selectionA;

//question
cin >> selectionA;

if (selectionA = 1)
{
//do something
}

else if (selectionA = 3)
{
//do some other thing
}

else

// rest of program


I know I'm missing the if selectionA > 3 thingy, but that's not important as this is just for entertaining half a dozen kids

Thanks all.
First of all, your if statements are wrong: they will always succeed whatever number is given. This is because 'selectionA' is being ste to 1 in the first one rather than compared: use == rather than =.

As for how to do it, simply use a while loop.
1
2
3
4
5
6
7
8
9
int selectionA = 0;

// while the correct answer isn't given
while (selectionA != 2) {
    std::cout << "1, 2, or 3? ";
    std::cin >> selectionA;
}

// rest of program 
Thank you. I followed as per your suggestion, however, there appears to be a bug. If I enter in the following order : 1, 3, 2. This happens.


1
Wrong

3
Wrong

2
Wrong


2, still wrong, why?
Not sure why you would want to prevent the user from moving on until he/she entered the right answer. it would make more sense to ask questions then total up the correct answers at the end.

Anyway some drunken code (been at a party lol)...

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

#include <iostream>
using namespace std;

char GetOption();

int main()
{
	char option;

	cout << "Hey answer 2 or your not getting past...  :";
	do
		option = GetOption();

	while (option != '2');  // loop if we aint 2

	// if we get here the function to read the keyboard
	// returned 2  :)
	cout << endl << "Well done, you got there in the end.";
	return 0;
}

// function to only accept 1 - 3 from keyboard
char GetOption()
{
	char ch;
	do
	{
		// get input
		cin.get(ch);  // read from the keyboard

	} while (ch < '1' || ch > '3');	 // loop if not 1 - 3
	return ch;  // return what i entered to caller
}


Thanks, Softrix. But I would be thankful if someone can build on NT3's suggestion.

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
	cout << "Everything that follows is a result of what you see here." << endl;
	cout << " " << endl;
	cin.get();

	cout << "Possible Replies:" << endl;
	cout << "1. Is there something you wanted to tell me?" << endl;
	cout << "2. Why did you call me?" << endl;
	cout << "3. What do I see here?" << endl;
	cout << " " << endl;
	cout << "Selection" << endl;
	cin >> selectionA;
	cout << " " << endl;



	while (selectionA != 2)

	{
		cout << "I'm sorry, my responses are limited." << endl;
		cin.get();
		cout << "You must ask the right question." << endl;
		cout << " " << endl;
		cin.get();
	}

	cout << "YOU: Why did you call me?" << endl;
	cout << " " << endl;
	cin.get();

	cout << "I trust your judgement." << endl;
	cout << " " << endl;
	cin.get();


current buggy output


Selection

1
I'm sorry, my responses are limited.
You must ask the right question.

3
I'm sorry my responses are limited.
You must ask the right question.

and this is where it goes wrong

2
I'm sorry, my responses are limited.
You must ask the right question.



If anyone finds this familiar, yes, this is the conversation Will Smith (as a homicide detective) had with a holographic projection of a dead scientist, in the film about a decade ago, call "iRobot".
1
2
3
4
5
6
while (selectionA != 2)
{
    cout << "I'm sorry, my responses are limited.\n";
    cout << "You must ask the right question." << endl;
    cin >> selectionA;
}

Also, what's up with all of the cin.get(); calls?
I can't tell if they're there in order to "pause" the program for the user to press Enter or if you're trying to get input with them. (If you are, just note that since you're not storing it to anything, you're essentially just throwing out the first character the user enters.)
Topic archived. No new replies allowed.