More then one word stored in a string

How do you do a strin(header <cstring>) that you can use two works. because im making a text adventure and i want to do "flick lightswitch"
Strings can store multiple words. The issue here is that you are using std::cin, which only takes the text up until the first space into the variable. What you want to do is use getline to get more than one word.
closed account (3qX21hU5)
Also since I am guessing you are using C++ (You are on a C++ forum) you should be using <string> instead of <cstring>.

If you are getting the words from the user you will need to use getline() in order to get the two words and store them in one string. Here is a example

1
2
3
4
5
string sentence;

// This will read everything into sentence until it hits a newline char (\n) which
// happens when you press enter.
getline(cin, sentence);


You can also change it so it stops reading after a certain character by specifying a third argument.


Now if you are just hard-coding the words "flick lightswitch" into a string all you need to do is this.

string words = "flick lightswitch";

Simple as that.

If you need any more help or need better examples please post your code so we can actually tell what you are doing.
Last edited on

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
#include <iostream>
#include <cstring>
using namespace std;

int main(){
	char action [6];


	cout<<"################################################################################";
	cout<<"#                                                                              #";
	cout<<"#                           Beast's Text Adventure                             #";
	cout<<"#                                                                              #";
	cout<<"################################################################################";
	cout<<"\n\n\n\n";
	cout<<"You are in a dark room, you cant see anything except some light shining from \nsome doors.\nThere is two doors. One to the north. One to the south.";
    cout<<"\n\nWhich way do you want to go ";
		 cin>>action;
	//First room north
	  if ( std::strcmp( action, "north" ) == 0 ){
		  cout<<"\nIt leads to another room. This room is lighter then the last. You can make out alightswitch to the left of you.";
		  cout<<"\n\nWhat do you want to do ";
		  cin>>action;
		//This is the line that does not work                                                             
  if ( std::strcmp( action, "flick lightswitch") == 0){
                //^^^This is the line that does not work 
}

		//First room south
	  }else if ( std::strcmp( action, "south" ) == 0 ){
		  cout<<"\nThis room looks the same as the last except there some light breaking through\nsome red velvet curtains.\n";
	  }
		system("pause");
	return 0;
}
Last edited on
closed account (3qX21hU5)
I would highly suggest you learn about C++ strings which will make this much easier for you. If you are going to use C++ you might as well use the tools it provides. Most of the time in C++ you want to avoid using C-Strings for many reasons.

Look at line 22 what did we say to use instead of cin? Maybe try using it?

Remember we are here to help guide you through your problem not to solve them for you.
Topic archived. No new replies allowed.