Latest Uploads
Extraterre ... .0.1 (zip)

dantheman363

Monty Teas ... Screenie 1

steve_ancell

Santa Clau ... ed his bag

waroffice

manic_platdude.png

spinal

Tetris Clone

steve_ancell

Super blues bros.

spinal

Forum Home

yes/no

UserMessage
Posted : Friday, 08 July 2011, 13:42 | Permalink | Mark Here
jedimastersterling


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?
Posted : Friday, 08 July 2011, 13:49 | Permalink | Mark Here
shroom_monk


WW Entries : 8
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!
Posted : Friday, 08 July 2011, 14:51 | Permalink | Mark Here
jedimastersterling


ok, i tried it now it says that i can't make comparisons between pointers and integers
Posted : Friday, 08 July 2011, 14:54 | Permalink | Mark Here
shroom_monk


WW Entries : 8
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!
Posted : Friday, 08 July 2011, 15:10 | Permalink | Mark Here
HoboBen


WW Entries : 9
(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)


-->



-----
github
Posted : Friday, 08 July 2011, 15:13 | Permalink | Mark Here
shroom_monk


WW Entries : 8
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!
Posted : Friday, 08 July 2011, 15:16 | Permalink | Mark Here
Jayenkai


WW Entries : 103
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!

-----
Posted : Friday, 08 July 2011, 19:12 | Permalink | Mark Here
jedimastersterling


HoboBen's sugestion worked perfectly. Thanks man.
Latest Posts
Consumer Lockout
9572AD Thu 06:09
Professor Oak, Honest!!
spinal Wed 23:24
Position in Mind
steve_ancell Wed 20:11
Sonic Adventure v0.x
Jayenkai Wed 07:08
Coder's Block
Jayenkai Wed 06:03
FIRST!!!
steve_ancell Wed 03:55
Noel's Graduation
waroffice Wed 02:55
Monkey vs iOS tweaks
spinal Tue 22:51
Fucking Pound Sign Unicode Bullshit Bollocks
Dabz Tue 13:00
Progress / Location Bars
dna Tue 08:55
More

Latest Items
News : Newsletter #176
Jayenkai Sat 04:49
News : Newsletter #175
Dabz Tue 09:38
Blog : Snow: More Material Junk
Cower Sat 23:17
Dev-Diary : Mutant Monty: Amstrad CPC to Windows conversion
rockford Fri 13:14
Techy : AppleTV
Jayenkai Thu 09:40
Blog : Graphviz
steve_ancell Sat 14:17
Pets : Top-Down Shadow Hack
Jayenkai Tue 05:52
Snippet : JNKrunch v1.0
Jayenkai Sat 07:20
News : Newsletter #173
waroffice Fri 04:47
Blog : Material Loading
Cower Fri 02:08
Pets : I Done Won A Thing
shroom_monk Sun 11:31
Pets : Repurposing A Lexer
Cower Mon 22:06
Bah : Feeling a Little Angry
spinal Mon 11:26
News : Newsletter #170
Dabz Sat 00:34
Showcase : sbfgen
Cower Sat 16:57
More

Who's Online
Krakatomato
Thu, at 07:00
Dabz
Thu, at 06:55
spinal
Thu, at 06:45
9572AD
Thu, at 06:13
shroom_monk
Thu, at 05:16
Jayenkai
Thu, at 05:13
steve_ancell
Thu, at 05:07
rockford
Thu, at 03:55
CodersRule
Thu, at 03:49
Cower
Thu, at 00:50
Link to this page
Site : Jayenkai 2006-Infinity |
MudChat's origins, BBCode's former life, Image Scaler.