Spaces and string

Started by
11 comments, last by snk_kid 19 years, 8 months ago
If I'm using the string class, how would I retrieve input from the user that has a space in it? it seems if I use std::cin it only gets the first half of the string, I can't use gets() because the string.c_str() function makes the string a constant, and I can't use the getline() because it requires a string length, something that I don't know (and is one of the reasons I'm using the string class). I could input it to a character array then put that in the string, but that kind of defeats the purpose of having the string class in that you have a limited number of characters. thanks!
Advertisement
std::string line;
std::getline(std::cin, line);
[size=2]
You can use getline, the count parameter defines the maximum number of characters to read, not necessarily the total length of the string to be read.

We are the music makers and we are the dreamers of the dreams. - WonkaAsking Smart Questions | BookPool
...so I just set it higher than what i would ever need...that works...thanks!
cin.getline(char *, int)

the second parameter is the number of character that we want to take.

example :

int main(int argc,char argv[][])        {        char *u;	c.clrscr();	cin.getline(u,12); // take 12 character	c<<u<<endl;}


i hope this will help.. hehehe..
You can't use istream::getline() with std::string, though. That's why in the <string> file there are defined global getline() functions. They take a std::istream reference as the first parameter, and a std::string reference as the second parameter, and optionally a delimiter. So you should use wild_pointer's version, if you want to read a line into a std::string object.

[edit: fixed awful grammar caused by previous edits]

[Edited by - Agony on August 17, 2004 2:37:14 PM]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
ok...thx again!
# include <iostream># include <string>using namespace std;int main(){   string myString;   cout <<"Enter a string with a space in it: ";   getline(cin, myString);   cout << myString;   return 0;}


If you have to push enter twice to get the output you've got something called the getline bug. Its an easy one time fix, just google for it.

If you use a regular cin before you use getline in your program you are going to do something like
cin.ignore(100,'\n');
or
cin.flush();
BEFORE you use getline.
This is because getline reads up to the newline character ('\n') ie the enter key. But cin leaves the newline character in the buffer so the getline following just reads the newline character not the string following.

Suppose you try to cin then getline the following input..
Goodbye\n
Cruel World\n
Where \n represents the enter key
cin just grabs "Goodbye"
when you press enter the second time this is what is in the buffer...
\nCruel World\n
getline reads only till the first \n
leaving Cruel World\n still in the buffer... oops!
Putting cin.flush() between the cin and getline blows that \n character right out of there and lets the getline work like you would expect it to.

I went into such detail because when I was first learning C++ this little quirk made me pull my hair out and until I finally figured it out.
Original post by gohacker82
char *u;cin.getline(u,12); // take 12 character


Uh, I don't think you want to be doing that. . .
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
hahaha..

using string is more efficient ya..

thanx for telling me...

^o^

This topic is closed to new replies.

Advertisement