I am new to C ++ and am making a simple text RPG. Anyway, the scenario: I have a welcome screen with options 1-3, and you have a simple IF statement to check them here:
int choice;
std::cout << "--> ";
std::cin >> choice;
if(choice == 1) {
This works fine, but if someone enters a letter as a choice (instead of 1, 2 or 3), it will become “-392493492” or something else and the program will crash. So I came up with:
char choice;
std::cout << "--> ";
std::cin >> choice;
if(choice == 1) {
This works fine, but when I enter a number, it seems to completely skip IF statements. Is char "1" the same as number 1?
I get a compiler error with this (ISO-CPP or something else):
if(choice == "1")
So, how do I see if they entered 1-3 correctly ??
source
share