Sign in to follow this  
NewbJ

Spaces and string

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;
c.clrscr();
cin.getline(u,12); // take 12 character
c<<u<<endl;
}




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

Share this post


Link to post
Share on other sites
No, because no memory was allocated for the char*, meaning that the location it points to could be reserved and cause an access violation when you try to write to it.

Share this post


Link to post
Share on other sites
Quote:
Original post by j0seph
Why? Just because char * is C and not C++? Will it mess with c++ compiler?


No you can use both C-style strings & C++ style strings.
C-style strings != C++ style strings thou. There are lots of advantages to using the latter over the former, most notably type safe efficent handling of memory mangement for you. Probably all methods are inline so the string type is basically as efficent as the built-in types.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this