Jump to content
  • Advertisement
Sign in to follow this  

Spaces and string

This topic is 5149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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;
cin.getline(u,12); // take 12 character

i hope this will help.. hehehe..

Share this post

Link to post
Share on other sites
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]

Share this post

Link to post
Share on other sites

# 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
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..
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!