Hello,
I'm working on some kind of a text-based game, and I'm trying to move on from the options being given, and the user selecting "1 or 2", to actually reading a string that was inputted and pick up key words. So far, I've learned how to read a whole sentences, with spaces, using the getline() function. Now, I want to know how I can process the stuff that was written into the string. Any help would be appreciated! Thanks!

Here's a little example:

#include <string>
#include <iostream>

int main(){
    std::string answer;
    std::cout<<"1 or 2";
    std::cin>>answer; // here you take the input, single word, till you hit enter, or ' ' is met.
    if (answer=="1" or answer=="1.")
        std::cout<<"one\n";
    else if (answer=="2" or answer=="2.")
        std::cout<<"two\n";
    else 
        std::cout<<"Invalid choice.\n";
    return 0;
}

Have a look at the cin function: Click Here.
Remember that you used to get the whole line by calling getline(cin, somestring);.
cin represents the standard input stream.

Edited 4 Years Ago by Lucaci Andrew

Hello, thanks for the answer. I know how to use the getline() function with cin, but my question was, how do I process that after it? Can the if statement be specified to pick up only some words or phrases?

Sorry for triple post, but I have another question. Using the method you listed, I am only able to execute the next line of the program if the input is exactly what is in the if condition. How do I make it so that it will execute if it has any one of the words in the string? Thanks again.

cin>> method gets the input till it "hits" the '\n' character (you press ENTER) or the space character ' '. getline(cin, somestring) gets the whole input till it "hits" the '\n' character. So, if you're using cin you'll get only one word. If you're using getline you'll get the whole line.

Ah yes, thanks for the reply. I was already aware of this, which i s why i chose to use the "getline" functino. However, my question was, how can I pick up key words from a whole sentence answer. LEt's say the game said:
"You are in front of the door, what do you do"
then the person says:
"Open the door."
How can I make it so that is just picks up the words "open" and "door", to execute the next line, instead of making the condition work for the whole sentence with hte exact words and spacings. (like we have now)
Thanks again!

Well you can see if the line from the user contains the words open and door. Something like this

if(userInput.find("open") && userInput.find("door"))
    // open door

You can also get a little more complicated and make sure you find open before the word door.

Thanks for the reply, I tried this in my program listed below:

#include <iostream>

using namespace std;

int main()
{   string test;
    cout<<"say something"<<endl;
    getline(cin,test);
    if(userInput.find("open") && userInput.find("door")) {
    cout<<"correct!"<<endl;
    }
}

However, when I do this, it gives me an error saying: 'userInput' was not declared in this scope|

I'm guessing that I need another "include" statement? Which library is it? Thanks again.

userInput was just a variable name i used. In your case you would want to use test instead of userInput

if(test.find("open") && test.find("door"))

Hey, thanks again! Now, something weird is happening. It says "correct" when I put anything but a phrase with the words "open" and "door" in it. Is it the way I built the if statement that could cause this?

Thanks again for the reply, but doing this didn't do anything. It doesn't reply "correct" even if anything is entered.

    #include <iostream>
    using namespace std;
    int main()
    { string test;
    cout<<"say something"<<endl;
    getline(cin,test);
    if(test.find("open") && test.find("door")) {
    cout<<"correct!"<<endl;
    }
    }

The problem with this one is that it says "correct" when I put anything BUT the words "open" or "door." But when I do enter a phrase with those words, it just exits.

The problem with this one is that it says "correct" when I put anything BUT the words "open" or "door." But when I do enter a phrase with those words, it just exits.

Well the problem here is one of the logic. In c++ the false boolean value is 0, and the true value is something different from 0. In your example

 if(test.find("open") && test.find("door"))

evaluates to true even thou those words are not found, because the test.find() function returns an integer, if the string is found, the 1st starting position in the searched string where the substring is, or if it's not found, the value of the string::npos constant, which is 4294967295. In both cases, the answer will be greater than 0 (which is considered to be false), so:

 if(test.find("open") && test.find("door"))

will always evaluate with true.
What you can do is to see if the value returned from the call to the find function is different from that string::npos constat, and here's how you can do it:

#include <iostream>
using namespace std;
int main(){
    string test;
    cout<<"say something"<<endl;
    getline(cin,test);
    if(test.find("open")!=string::npos && test.find("door")!=string::npos){
        cout<<"Correct!\n"<<endl;
    }
    else
        cout<<"Not.\n";
}

Hey! Thanks for the answer, it worked for the most part, but when I put in more conditions for it to be true, it keeps going to the "else" part of the statement.

#include <iostream>
using namespace std;
int main(){
    string test;
    cout<<"You're trapped in a closet, what do you do?"<<endl;
    getline(cin,test);
    if(test.find("find")!=string::npos && test.find("key")!=string::npos && test.find("look") !=string::npos){
        cout<<"You found a key!"<<endl;
    }
    else
        cout<<"Unknown command"<<endl;
}
This article has been dead for over six months. Start a new discussion instead.