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.";
        }
    }

    HUDK();

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

Recommended Answers

All 8 Replies

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.

What is the type of the variable Input?

their 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

getline(cin,Input);

^^^^ 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.

**string Input;

if (strcmp(Input, "Pick Up Knife") == 0)
{
    cout << "You got the knife" << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.