Im trying to get this working but my if statments dont work or am i just doing them wrong ?

cout << "You see a house, what do you do?\n1. Run away\n2. Walk inside" << endl;
    cin >> Input;
    if (Input == "Run away"){
        PowerLevel_Final = PowerLevel_Final - 9036;
        cout << "You got hit by a tree, this drained your power level"<< PowerLevel_Final << endl;
        cout << "You have died." << endl;


        else if (Input == "Walk inside"){
        PowerLevel_Final = PowerLevel_Final - 35;
        cout << "You walked into the house.\nThis drained your power level." << PowerLevel_Final << endl;
        cout << "You see a knife, what do you do?\n1. Pick up the knife\nWalk past the knife." << endl;
        cin >> Input;
        if (Input == "Pick up the knife"){
        cout <<"Knife has been added to your inventory.";

        else if (Input == "Walk past the knife"){
        cout << "You have died.";


Also can i just make it so if the Input is equal to one make it start reading from another Cpp file

4 Years
Discussion Span
Last Post by rubberman

They won't work due to the == operator. The "strings" are really just character arrays. When you use the == operator, you're comparing the memory addresses, not the characters. Take a look at strcmp in the cstring header.

If you're used to working with C# (or possibly Java, but I can't remember how Java handles string comparison), class comparison (which strings are) is generally done the same way (except their object points instead of a memory address), however the .NET framework overloads the == operator to compare the value of strings.


Are they the string type from the <string> header? If they are the == operator doesnt ignore case so if you input "pick up the knife" it wont work.


you have to do


^^^^ this gets the spaces in the user's input so from there you can do your

if(Input == "Pick Up Knife") 
   cout << "You got the knife" << endl; 

Also make sure you did

#include <string>

String Input;

This can also be something that messes up your program. I have to do this for my Visual Studios 2010.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.