Spaces and string
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!
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.
cin.getline(char *, int)
the second parameter is the number of character that we want to take.
example :
i hope this will help.. hehehe..
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]
[edit: fixed awful grammar caused by previous edits]
[Edited by - Agony on August 17, 2004 2:37:14 PM]
# 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 gohacker82char *u;cin.getline(u,12); // take 12 character
Uh, I don't think you want to be doing that. . .
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement