[easy] problem, while-loop

The following Code is working. But in the book, I am studying it is said, that I should read in two numbers. And when the character 'N ' comes, the while-loop should stop. So now, I am not sure about the way I solved this problem, because I think the code should not have a cin >> stop; and stop automaticcaly when something other than a int-valueget got written in.
Can anybody explain me how I can solve this problem? Sorry for my bad english...



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using namespace std;
int main()
{	
	int number_1 = 0;
	int number_2 = 0;
	char stop = 'a';

	while (stop != 'N'){
   cout << "Enter two numbers:" << endl;
   cin >> number_1 >> number_2;
   cout << number_1 << ' ' << number_2 << endl;
   cout << "Would you like to continue with writing in Numbers? Write Y or N:" << endl;
	cin >> stop;

	}
	
	return 0;
}


Last edited on
Is the program not stopping when you enter the letter "N"?
Under
1
2
cout << "Would you like to continue with writing in numbers? Write Y or N:" << endl;
cin >> stop;


Type this to make it stop:
if (stop == N) break;

You can make the while infinite by doing this:
1
2
3
4
while (true)
{
 // loop goes here
}
Last edited on
Ignore DetectiveRawr , he is in no position to help anyone, seriously bad adivice on both counts.
1
2
3
4
5
char yn = 'y';
do {
	cout << "looped message" << endl;
	cin >> yn;
} while (toupper(yn) == 'Y');

perhaps?
Topic archived. No new replies allowed.