Why wont this compile
Jun 5, 2010 at 4:32pm UTC
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" );
}
Jun 5, 2010 at 4:36pm UTC
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
Jun 5, 2010 at 4:51pm UTC
Centurion210 is right. Also, change = to ==. = is for assignment, == is for comparison.
Jun 5, 2010 at 5:01pm UTC
didnt notice that in his code, thx for the heads up, hamsterman!
Jun 5, 2010 at 6:27pm UTC
thanks guys, i put both suggestions in and it works now.
Jun 6, 2010 at 4:09pm UTC
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" );
}
Jun 7, 2010 at 2:34pm UTC
I believe this happens because cin.get doesn't remove the last \n in the stream.
Try adding cin.ignore after it.
Jun 7, 2010 at 2:41pm UTC
Topic archived. No new replies allowed.