123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|731|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> yes/no

Fri, 08 Jul 2011, 14:42
jedimastersterli
Simple question and you are going to think i'm stupid for asking but i'm just getting my feet wet.

You know how in old dos programs the computer asks for a yes or no response by saying something like kdsajfs jhskhafds? y/n. I tried to implement that in a C++ program like so

char response;
cout <<"askjdune kksed? y/n\n";
cin>> response;
if (response == y)
else if (response == n)
else

the problem is the computer wants to treat y and n as variables. So how do you make if comparisons for characters responses?
Fri, 08 Jul 2011, 14:49
shroom_monk
Anything you want read as a string needs to be in double quotes. Without them, y and n are treated as variables. So your above code should be:

See the quotes on lines 4 and 5? That should do it.

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Fri, 08 Jul 2011, 15:51
jedimastersterli
ok, i tried it now it says that i can't make comparisons between pointers and integers
Fri, 08 Jul 2011, 15:54
shroom_monk
Hmm... what does your entire code look like now? Exactly as the above?

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Fri, 08 Jul 2011, 16:10
HoboBen
(This is true for C, I assume this is true for C++)

As it's just a char, you'll want to use single quotes.

"a" with double quotes is a string literal (like "abcdef").

'a' with single quotes is a character (so 'abcdef' would be invalid).

The compiler is complaining because "y" with double quotes is of type "const char *", whereas 'y' with single quotes is of type "char" (which can be compared to your char response variable).

This is why the compiler printed an error about making comparisons between pointers and integers. The response variable of type char is a type of integer, and "y" is a pointer to a string constant. Therefore, you were making a comparison between a pointer and an integer (which is not what you wanted in this case).

Easy fix:




If you use a char array for a full string, you use the strcmp function (look up strcmp for more information)






-=-=-
blog | work | code | more code
Fri, 08 Jul 2011, 16:13
shroom_monk
Ah, I hadn't thought of that. Silly me. I use std::string for most of my string ops anyway, so I forget the subtleties of char. /excusemaking

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Fri, 08 Jul 2011, 16:16
Jayenkai
To clarify, because for a long time I used to go "Well, why the hell is a single letter different!?!?"..

"a" = The word "a"
whereas
'a' = the number 97, which is the ascii code for the letter a.

You're better off with using strcmp, like HoboBen says, because it's much less confusing that way. (well, kinda!)

Don't expect C to do nice string things like every other language on the planet. It won't!

-=-=-
''Load, Next List!''
Fri, 08 Jul 2011, 20:12
jedimastersterli
HoboBen's sugestion worked perfectly. Thanks man.