Link to home
Start Free TrialLog in
Avatar of SuperSid
SuperSid

asked on

How to use a string with a space?

I need to know how to use a string with a space.

I did

string s;
cin>>s;

when i typed in "Hello World"
and i do:

cout<<s;

it only shows "Hello", but the World disappeared.  How do i make it so that both words or more will be include in s?

so in the future i could use

if (s == "hello world")




Also how should i create a simple code so that when i asked the user to input a string, and any word in that string will be in the "if" statement.

for example, i asked the user to cin>>s;

if the user inputs "go to bathroom"

I want an If statement so that if any word in the string "s" contains the word "bathroom", the if statement will execute.


e.g. the user inputs "go to bathroom" or "bathroom" or "go in bathroom" , the if statement will executes.

What should the if statement look like?
Avatar of DarthNemesis
DarthNemesis

cin only takes one word at a time. You can use getline(cin, s); to input a whole line.

As for finding a certain string, if you want it to accept exact words only, one of the easiest ways is to use a stringstream.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

void main() {
        string x;
        getline(cin, x);
        stringstream s;
        s << x;
        string find;
        while (!s.eof()) {
                 s >> find;
                 if (find == "bathroom") {
                        // add your code here
                 }
        }
}
Avatar of SuperSid

ASKER

Hehe i'm really new to c++ and don't know a lot of codes.

I sorta know what getline is, but not stringstream. Could you add comments on what they do?  like that s<<x and the (!s.eof())

thx
Hehe i'm really new to c++ and don't know a lot of codes.

I sorta know what getline is, but not stringstream. Could you add comments on what they do?  like that s<<x and the (!s.eof())

thx
i think cin's getline takes a parameter char*, not string so basicly he can just do
char* str = new char[80];
cin.getline(str);
then to compare this just use strcmp(char*, char*)
in this case strcmp(str, "hello world");
strcmp returns 0 if they're equal, -1 if the 2nd > 1st and 1 if 1st > 2nd
Hehe i'm really new to c++ and don't know a lot of codes.

I sorta know what getline is, but not stringstream. Could you add comments on what they do?  like that s<<x and the (!s.eof())

thx
ASKER CERTIFIED SOLUTION
Avatar of DarthNemesis
DarthNemesis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I don't know how much you've learned about classes, so I'll clarify one more thing:
Classes are a collection of variables and functions designed to make coding simpler or more organized. The stringstream class is made up of several 'members' - a buffer and the two pointers - and has a bunch of functions that let you interact with them. The . operator accesses a member or member function of a class, so s.eof() means run the function eof() that's a member of stringstream s. The ! operator just reverses a boolean condition, i.e. !true is the same as false. So the condition (!s.eof()) means that s is not at the end of its buffer, and that there are more strings to read from it.
DarthNemesis, sorry I didn't read the last part of his question =) I think you've got the answer to his question.
sorry about that repeated post from hitting the Refresh button.

hahah it's a lil complicated, but i'm trying my best to understand.

could you write a sample code to simply ask the user to input any words, and if one of the words contain the word "bathroom", it will execute a function?  or just an exact word say "go in bathroom".  I tried doing

string ans;
cin.getline(ans);

but some kinda error occurred.

also
getline (cin, ans);  

when i execute this, it lets me enter the string Twice.  and it seems like for every word my If statement occurs or something and i'm confused hahah.  So could u write the simplest function to ask the user to input any number of words, or exact words if that's easier, and write an if function so that the 'ans' contains the word "bathroom" or just exact words "go in bathroom"?
if my question is confusing, this is how the code i think should look like.

cout<<"You are in your room.  There is a door to the bathroom, and a door to the living room.\n";
cin>>ans; //this is the part i don't get.  How do i get the user to input any numbers of words?

if (ans == "bathroom") //this If statement checks for if the 'ans' contains the word "bathroom" but i don't know how to write it.

cout<<"you have entered your bathroom\n";

once executed i want it to look like:

You are in your room. There is a door to the bathroom, and a door to the living room.
(enter something)
e.g. Go into the bathroom (or go in bathroom or get in bathroom)

You have entered your bathroom.
AHhhh i think i got the

while (!s.eof())

statement that you wrote.

But still the

s<<x

and

s>> find

are still a little confusing.  Could someone explain this?  i know very little about c++.
like DarthNemesis said, s << x will put the content of the string x into the buffer that stringstream creates like so
before: _ _ _ _ _ _ _ _ _ _ _ ...
after:  h e l l o _ w o r l d
then using the extractor >> we get part of the buffer to the variable, i.e. doing s >> find will make this to happen:
before: find is "empty" and hello world is in buffer.
after: find contains "hello" and the pointer in the buffer stops in front of "world."
does that make any sense?
Yup i got it.  Now the getline (cin, s) makes me input 2 lines.  THis is the code:
       cout<<"Enter something\n";
       string ans;
       getline(cin, ans);
       stringstream s;
       s << ans;
       string find;
       while (!s.eof())
          {
                s >> find;
                if (find == "bathroom")
               {
                          //code
                        }
                }

     


when executed, it goes like this:

Enter something.
(i entered "go in bathroom" and hit ENTER)

then it waits for me to enter something again.
(then i entered anything)

why does it do this?  Shouldn't it ask me only once?
I've had that happen before, but only if I used cin previously. Do you have any code before what you posted?

When you use cin << s, if the user types more than one word, the cin buffer will still have some data left over. If you try to use getline after that, the program can mess up - it might ask you twice, or skip over it completely. To avoid this, you have to empty out the cin buffer. The easiest way to do this is using ignore() - after your cin, put cin.ignore(100); (some large number).
how do you use a cin.ignore(100)?  

i did

cin>>s;
cin.ignore(100)

and when executed, it wouldn't stop letting me cin.
Hmm... try cin.ignore(100,'\n');
i assume s is a string?
this is, like DarthNemesis said, because there are "stuff" from previous entries remaining in the buffer, which is most likely the "enter" part that was not taken cared of.
How about trying:
cin >> s;
cin.get();
that's it!  that's the command!  thanks!
cin.ignore(100,'\n');
that's it!  that's the command!  thanks!
cin.ignore(100,'\n');
Maybe you should lay off the refreshing for a while... =P