Detecting Specific User Input.

Hello,

I'm trying to get specific user-input from the user, and I want the user to enter certain words in order to go to other parts of the program. Can anyone tell be the best way to do this? I've built and run my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cout << "START GAME " << endl;
cout << "OPTIONS " << endl;
cout << "CREDITS" << endl;
cout << "EXIT " << endl;
std::cout << ">";
cin >> main_menu;
if (main_menu.find ("START GAME") != string::npos)
{
int startGame();
}
else if (main_menu.find ("OPTIONS")!= string::npos)
{

int Options();

}

else if (main_menu.find("CREDITS")!= string::npos)
{

int Credits();

}
else if (main_menu.find("EXIT")!= string::npos)
{
 exitGame();
}

But, unfortunately, when I enter the words (in caps), it dosen't direct me to the function. It just closes the program... Can anyone help me on this? :(
(Btw main_menu is a string)
I tried your code and apparently it does not work for string containing spaces (like "START GAME") but it works for the others?
In your implementation the other options are not selectable?
If that's the problem until a better solution use "START_GAME" for example.
Now I remembered! The problem is with this:
cin >> main_menu; cin strips down the words of their spaces. You never enter "START GAME" just "START"!

Use anther input method like:
getline (cin, main_menu);
I tried your solution and it works.... part-way.

This time,I type in a word and it just displays my:
exitGame() function.
Any other suggestions?
(EDIT: A bit Zork-like...)
Last edited on
I'm guessing my question isn't clear:

I'm trying to get the user to enter certain command-prompts in order to get to other parts of my functions. A bit Zork-Like where the user has to enter "pick apple up", in order to interact with the program... Anyone have any suggestions as to how I can do this?
Last edited on
Nevermind...
Topic archived. No new replies allowed.