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!
Carpetfizz
0
Light Poster
Recommended Answers
Jump to PostHere'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"; …
Jump to Post
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 usingcin
you'll get only one word. If you're usinggetline
you'll get the whole …
Jump to PostWell 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.
Jump to Posttry this
if(test.find("open") != string::npos && test.find("door") != string::npos)
Jump to PostThe 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 …
All 18 Replies
Lucaci Andrew
140
Za s|n
Carpetfizz
0
Light Poster
Carpetfizz
0
Light Poster
Carpetfizz
0
Light Poster
Lucaci Andrew
140
Za s|n
Carpetfizz
0
Light Poster
NathanOliver
429
Veteran Poster
Featured Poster
Carpetfizz
0
Light Poster
NathanOliver
429
Veteran Poster
Featured Poster
Carpetfizz
0
Light Poster
NathanOliver
429
Veteran Poster
Featured Poster
Carpetfizz
0
Light Poster
Carpetfizz
0
Light Poster
NathanOliver
429
Veteran Poster
Featured Poster
Carpetfizz
0
Light Poster
Carpetfizz
0
Light Poster
Lucaci Andrew
140
Za s|n
Carpetfizz
0
Light Poster
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.