views:

136

answers:

6

I am new to C++ and am making a simple text RPG, anyway, The scenario is I have a "welcome" screen with choices 1-3, and have a simple IF statement to check them, here:

int choice;
std::cout << "--> ";
std::cin  >> choice;

if(choice == 1) {
//..

That works fine, but if someone enters a letter as selection (instead of 1, 2 or 3) it'll become "-392493492"or something and crash the program. So I came up with:

char choice;
std::cout << "--> ";
std::cin  >> choice;

if(choice == 1) {
//..

This works kinda fine, but when I enter a number it seems to skip the IF statements completely.. Is the char "1" the same as the number 1?

I get a compiler error with this (ISO-CPP or something):

if(choice == "1")

So how on earth do I see if they entered 1-3 correctly!?

+1  A: 

if(choice == '1')

And to answer your question, the ascii valyue for 1 is not equal to 1, but is equal to 49:

'1' == 49
Andreas Brinck
+1  A: 

A value in double quotes is interpreted as a string (of type char*, which is incompatible with a char), while in single quotes it is interpreted as a char:

if(choice == '1')

The integer representation of the char'1' is not 1, but 49 (in ASCII). So you could also write

if(choice == 49)

Also, you should have an else branch to display an error message or something, and prevent the program from continuing in case an invalid input has been entered.

Péter Török
+1  A: 

choice is a char so you should use '1' to check. "1" represents a string with 1 character in it.

PoweRoy
+3  A: 
1 is an int
'1' is a char
"1" is a char array

I guess you want to compare with '1'.

Didier Trosset
+4  A: 

Choice doesn't become "-392493492" or something, it starts as that value (you didn't initialise it, so the initial value is unspecified) and is never set to anything else because the >> fails. You should check that such operators succeed, which is quite easy to do:

if (std::cin >> choice) {
    switch (choice) {
    case 1: // ...
    case 2: // ...
    case 2: // ...
    default: // report error
    }
}
Marcelo Cantos
The whole thing was within a while loop (to keep on checking until proper choice), I think I messed some of the break;'s up and it segfaulted the odd time, first time I got one, Maybe that integer was just some spit out error
Nullw0rm
No, the integer is essentially a random number. It's whatever number was in whatever memory cell happened to be allocated to that variable when you entered the function. Because the `>>` failed, the integer was left unmodified.
Marcelo Cantos
+2  A: 

Unfortunately 1 and '1' are not the same.

Look up your favorite ASCII table to know the integer value that represents the character "1" and you'll see it for yourself: '1' is mapped to 49.

There is another issue with this code "" denotes a C-string (const char*) whereas '' denotes a single character.

Here is your code reworked:

char choice = 0;
if (cin >> choice)  // check success
{
  switch(choice) // choose
  {
  case '1': {  /**/ break; }
  case '2': {  /**/ break; }
  case '3': {  /**/ break; }
  default:
    cout << choice
         << " is not a valid choice, please press 1, 2 or 3 and Enter"
         << endl;
  }
}

I switched to switch because it's more natural than a chain of else-if for generic enumeration.

Matthieu M.
Why is `choice` a `char` in your code? `int` would make more sense.
Konrad Rudolph
Thanks alot! This inspires me to get a good C++ book, tutorials online don't show much important things like this.
Nullw0rm
@Konrad: I personally thinks it makes little difference and Jason had elected `char` in its last try so I just followed suit. The only issue is to be consistent either go for `char` and compare it to `char`s or go for `int` and compare it to `int`s.
Matthieu M.