Why wont this compile



Can somebody please explain why this won't compile. It falls over at line 20 with

'cannot convert from 'const char [2]' to 'char'.

should i be using == somehow??


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

using namespace std;


int main()

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

	system("PAUSE");
		
}
Try to use single-quotes in Q instead of double and lets see how it goes, i'm without a compiler here but i think its that.

Cheers

Centurion210
Centurion210 is right. Also, change = to ==. = is for assignment, == is for comparison.
didnt notice that in his code, thx for the heads up, hamsterman!
thanks guys, i put both suggestions in and it works now.
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??.

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");
		
}
I believe this happens because cin.get doesn't remove the last \n in the stream.
Try adding cin.ignore after it.
Topic archived. No new replies allowed.