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