Program won't wait for input ??

Hello again, i've a simple little program below that compiles ok and seems to work. Basically it asks you to input a letter until a 'Q' is detected and then quits. However, after the first cycle through the loop my Debugger wont wait for a keyboard entry again??.

Is this an IDE setting in Visual C++ ? or do i have to do something in the code itself ?.

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
#include <cstdio>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>

using namespace std;


int main()

{
	char st2[3];
		
	do 
	{

	  cout << "enter letter ";    
	  cin.get(st2,2);   // if i enter "Q" here...
	  cout << endl;
		
      if (st2[0] == 'Q')
	    { cout << "Q detected !" ;
		  break;
	    }

	  } while ( st2[0] != 'Q');

	system("PAUSE");
		
}
Use cin.getline(str2,2); instead.

Also, take a look at this -> http://cplusplus.com/reference/iostream/istream/get/
It works. Thanks
Here is the program doing the same task ....
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstring>
using namespace std;
int main()
{ 
   char ch;
   while (cin.get(ch))
   if (ch == 'Q'){ cout << "Q detected! " << endl; break;}
   cin.ignore(2);
   return 0;
}
Topic archived. No new replies allowed.