Working with loops...
Oct 29, 2008 at 12:55am UTC
I'm teaching myself C++ with a book I got and I'm working with loops with multiple branches. My code is an endless loop however. Please help.
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
#include <iostream>
using namespace std;
int answer1; //generic variable for general purpose choices
char answer2[1]; //variable for exiting the loop
int main()
{
cout << "INTRO" << endl;
do {
cout << "CHOICES 1,2,3" << endl;
cin >> answer1;
if (answer1 == 1)
{
cout << "Answer for choice 1 y/n Continue" << endl;
cin >> answer2;
}
if (answer1 == 2)
{
cout << "Answer for choice 2 y/n Continue" << endl;
cin >> answer2;
}
if (answer1 == 3)
{
cout << "Answer for choice 3 y/n Continue" << endl;
cin >> answer2;
}
else
{
cout << "Please input a valid answer" << endl;
}
}
while (answer2!= "y" );
cout << "Loop has been exited. Terminating program." << endl;
cin.get();
return 0;
}
I'm not too sure about how the char checking would go but I really would like to use y/n instead of numbers.
Oct 29, 2008 at 1:26am UTC
If you want single chars, go ahead and just cin a char, just know that the excess data they put in will be kept in the buffer...to check chars, just check like it's a string except use 'C' where C is one character.
Topic archived. No new replies allowed.