Multiple character problem

My problem is that the code only works properly when I type in one character at a time so if I type in 'k' I get

cout<<"Remember the program only understands y or n as an answer. It won't even understand yes or no"<<endl;
cout<<"Do you want to give it a new try?"<<endl;

But if I write two characters for example 'ko' I get the same output twice like this

cout<<"Remember the program only understands y or n as an answer. It won't even understand yes or no"<<endl;
cout<<"Do you want to give it a new try?"<<endl;
cout<<"Remember the program only understands y or n as an answer. It won't even understand yes or no"<<endl;
cout<<"Do you want to give it a new try?"<<endl;

HOw do I fix 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
  #include <iostream>
#include <iomanip>

using namespace std;

int main(){

	int x;
	char answer,answer2;

	do{
	do{
	cout<<"Type in an integer"<<endl;
	cin>>x;

	if(x<56){
		cout<<"YOU LOSE"<<endl;
	}

	else if (x>78){
		cout<<"YOU LOSE"<<endl;
	}

	else{
		cout<<"YOU WIN"<<endl;
	}
	cout<<"Wanna try again. Pres y for yes and n for no"<<endl;
	cin>>answer;
	}
	while(answer=='y');

	if(answer=='n'){
	}
	else{
		do{
		cout<<"Remember the program only understands y or n as an answer. It won't even understand yes or no"<<endl;
		cout<<"Do you want to give it a new try?"<<endl;
		cin>>answer2;
		}
		while(answer2 != 'y' && answer2 != 'n');
	}
	}
	while(answer2=='y');

	cout<<"Thanks for trying!";

	return 0;
}
After you type "ko", what do you want to have happen?
The same as when I type in one character fx 'k' so the output will be

cout<<"Remember the program only understands y or n as an answer. It won't even understand yes or no"<<endl;
cout<<"Do you want to give it a new try?"<<endl;
cin @ 28 & 38 is not always the best method. getch () would probably work better and if your using Linux, ncurses even gives a better alternative

1
2
3
4
5
    cbreak ();
    noecho ();
    answer = getch ();
    echo ();
    nocbreak ();


As soon as you press a key, your application will respond and it won't echo character onto screen.
Topic archived. No new replies allowed.