Trying to split an input into two separate strings for two different functions?

So I'm just getting into C++ programming, and I decided to go ahead and make a very short text adventure game. My goal is to make it so that when the user inputs a line with two words (like "use door" or just "look") it would take the first word, and use that as a function and then take the second word and use it in that separate function. Can someone give someone with around 4 hours of C++ experience and some code from a friend some help?

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "stdafx.h";
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

//Set some global variables

bool g_Free = false;  //boolean telling us if you're free from the cell
int bar = 0;       //bar counter telling us if the user has the bar from the window or not


//Make some functions
string object(string *input)  //this is supposed to declare the object string used to identify what the action is being used on (ex. "look DOOR"
{
	
}

string action(string *input)  //this is supposed to declare the action, used in the first word of user input (like "LOOK door"
{
	std::string str ;  //declares input as "str"
	
}	
	

void DoProcess(string input) //this is the function that will be used to decide which function should be used next, based on the action string that was recieved from the user's input
{
	string cmd = action(&input);
    if( cmd == "look" ) OnLook(object);  //if action is look, run function OnLook
    else if( cmd == "use" ) OnUse(object);  //if action is use, run function OnUse
	else if( cmd == "help" ) OnHelp(object);
    else 
	{
		cout << "Silly drunk, what are you doing? Try to do something productive, like getting out of this cell!" << cout;
    }
}

void OnLook(string arg)    //function that runs if the user's action is look
{
	if(arg == "door") 
	{
		cout << "Apon looking at the hulking wooden door, you notice that there is a small clearing between the door and the wall.  Almost the right size for a small object, like a bar..." << endl;
	}
	else if(arg == "window")
	{
		cout << "Observing the window, you notice that one of the bars is coming loose.  Maybe you could use it later?" << endl;
	}
	else
	{
		cout << "You seem to be in a small room, almost like a prison cell.  The only things in it are a small window and a large wooden door." << endl;
	}
}

void OnUse(string arg)    //function that runs if the user's action is use
{
	if (arg == "window") 
	{
		string answer;
		cout << "You notice that a bar in the window is somewhat loose, would you like to take it?" << endl;
		cin >> answer;
		if(answer == "yes")
		{
			cout << "You take the bar out of the window!" << endl;
			bar = bar + 1;
		}
	}
	else if(arg == "door")
	{
		if(bar == 1)
		{
			cout << " You slip the bar through the door and successfully escape! Congradulations!" << endl;
			g_Free = true;
		}
		else
		{
			cout << "The door is locked silly, you need to find a way to open it!" << endl;
		}
	}
	else 
	{
		cout << "Silly drunk, what are you doing? Try to do something productive, like getting out of this cell!" << endl;
	}
}

void OnHelp(string arg)  //function that runs if the user types in "help"
{
	if(arg == "help") 
	{
		cout << "Use LOOK to look around, use OPEN to open things, and use USE to use things" << endl;
	}
	else
	{
		cout << "Silly drunk, what are you doing? Try to do something productive, like getting out of this cell!" << endl;
	}
}

int main()
{
	string input;
	while (g_Free == false)
	{
		
		//placeholder for some SICK ASCII ART


		cout << "You wake up in a small room, clueless as to what happened last night." << endl;
		cout << "Maybe you had a little too much to drink at the tavern last night." << endl;
		cout << "You do remember that you needed some HELP getting out of the tavern..." << endl;
		cout << endl;
		cout << "What would you like to do?" << endl;
		cin >> input;
		DoProcess(input);
	}
}
Also, my god, sorry for the hulking code. I just need some direction, and any would be appreciated, hahaha...
closed account (jvqpDjzh)
to split a string into tokens you can use strtok from <cstring>. See here how it works: http://www.cplusplus.com/reference/cstring/strtok/?kw=strtok
So something like this to grab my action/object?

1
2
3
4
5
6
7
8
9
10
string object(string *input)
{
	char str[] = (input);
	char * pch;
	pch = strtok (str," ");
	while (pch != NULL)
	{
		pch = strtok (NULL, " ")
	}
}
You could use a stringstream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <sstream>

    using namespace std;

int main()
{
    string input, word;
    cout << "please type a sentence" << endl;
    getline(cin, input);
    cout << "The words are:\n";
    
    istringstream ss(input);
    
    while (ss >> word)
    {
        cout << word << endl;
    }
    
    return 0;
}
please type a sentence
use door
The words are:
use
door


Instead of cout at line 18, you might want to add the word to a vector, or call a function etc.
Last edited on
closed account (jvqpDjzh)
Yes, what Chervil has answer is probably better for your situation!
I see what you're trying to do with the stringstream, but how would you put the two words into two separate strings? This is what I have so far, it's basically what you posted. I just made it a function instead of putting it into the main function so streamline things.

1
2
3
4
5
6
7
8
9
10
11
12
void GetInput()
{
	string input, word;
	getline(cin, input);

	istringstream  ss(input);

	while (ss >> word)
	{
		cout << word << endl;
	}
}
If you knew that there were exactly two strings no more and no less, you might do:
1
2
3
4
5
    string input, first, second;;
    getline(cin, input);
    istringstream  ss(input);
    ss >> first;
    ss >> second; 


Though I'd tend to favour a more general approach, such as putting the words into a vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void GetInput()
{
    string input, word;
    getline(cin, input);

    istringstream  ss(input);
    
    vector<string> wordlist;

    while (ss >> word)
    {
        wordlist.push_back(word);
    }
}


Alright, so once the vector is made, how would you make it so the first word was directed into the DoProcess function and then the second word into whatever function is called afterward?
And also, I'm not fishing for answers, but I have almost no clue what I'm doing right now. I really appreciate the help, by the way.
At some stage you'll have to slow down a little bit and read up on the various things you're using.

http://www.cplusplus.com/reference/string/string/getline/
http://www.cplusplus.com/reference/sstream/istringstream/
http://www.cplusplus.com/reference/vector/vector/
http://www.cplusplus.com/reference/vector/vector/push_back/

Regarding the vector, use the size() function to find how many items it contains and the [] array notation to access each item.
http://www.cplusplus.com/reference/vector/vector/size/
http://www.cplusplus.com/reference/vector/vector/operator%5B%5D/
Last edited on
So from what I'm reading, vectors are like containers that store data that can change in size? If so I think I grasp how they work, could you tell me if this is how it's supposed to be written? And for some reason, in DoProcess, it's telling me that the strings are still undefined, even when they're defined in GetInput.

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
28
29
void GetInput()
{
    string input, word, action, object;
    getline(cin, input);

    istringstream  ss(input);
    
    vector<string> wordlist;

    while (ss >> word)
    {
        wordlist.push_back(word);
    }
	cout << wordlist.size();
	wordlist[0] = action;
	wordlist[1] = object;
}

void DoProcess(string input) 
{
	string cmd = action(&input);
    if( cmd == "look" ) OnLook(object);  //if action is look, run function OnLook
    else if( cmd == "use" ) OnUse(object);  //if action is use, run function OnUse
	else if( cmd == "help" ) OnHelp(object);
    else 
	{
		cout << "Silly drunk, what are you doing? Try to do something productive, like getting out of this cell!" << cout;
    }
}
There's always help available here, but it kind of seems like you're guessing. I think the tutorial pages might be a good idea for you to read through.
http://www.cplusplus.com/doc/tutorial/
That's true, I am somewhat guessing, but I can't seem to find any connection as to how I'm supposed to get the wordlist into different strings, and then import those strings into DoProcess from GetInput. Sometimes it's worked, other times it hasn't, but I can't seem to figure out why.
Here's how you can do it:
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
28
29
30
31
32
33
34
35
36
void GetInput()
{
    string input, word, action, object;
    getline(cin, input);
    istringstream  ss(input);
    vector<string> wordlist;
    while (ss >> word)
    {
        wordlist.push_back(word);
    }
	cout << wordlist.size();
	//this is what you did wrong.
	action = wordlist[0];//wordlist[0] = action;
	object = wordlist[1];//wordlist[1] = object;
	
    // or alternatively,
    //if you know there's only two words,you can do:
    //void GetInput()
    //{
    //	string action,object
    //	cin >> action >> object;
    //}
    //now you probably want to call DoProcess()
    DoProcess(action,object);
}

void DoProcess(const string& action,const string& object)//void DoProcess(string input) 
{
    if( action == "look" ) OnLook(object);  //if action is look, run function OnLook
    else if( action == "use" ) OnUse(object);  //if action is use, run function OnUse
	else if( action == "help" ) OnHelp(object);
    else 
	{
		cout << "Silly drunk, what are you doing? Try to do something productive, like getting out of this cell!" << cout;
    }
}

Hope that helps.

P.S You might want to convert the given input to all lower case.See tolower() in cctype
Last edited on
Topic archived. No new replies allowed.