Confused on choices.

Hello everyone, I am currently programming a text - based game in c++. It is a fantasy/medieval RPG. Now you are able to set a class type, the three classes are Guard, Archer, or Mage. Now I have a string variable for it, but i also want to make it so i can do while loops with it.

Like this

string name;

cout << "Hello what is your name?: ";
cin >> name;

// But then i want to make a loop with it like

while (name == Bob)
{
statement;
}

while (name == Joe)
{
statement;
}


Is this possible to do? I am just learning, so please help! Thank you !
Yes it is possible... and you're very close in the code you posted.

The thing is that this:

 
while(name == Bob)


Takes the variable name and compares it to the variable Bob. Since you do not have a variable named Bob this will error. And you don't want to compare it to a variable anyway.... you want to compare it to the actual string data of "Bob".

So to do that, you put it in quotes:

 
while(name == "Bob")


Now it will compare the variable name with the literal string "Bob".
Thank you so much!
closed account (Dy7SLyTq)
just fyi... in c++ you cant do a switch on strings without writing special code.

side note: that would be a nice thing to see on a standard
Topic archived. No new replies allowed.