Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Another string problem
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
halo3d




PostPosted: Sat Nov 11, 2006 10:45 pm   Post subject: Another string problem

well i got another problem..
in this thing when user enters a long string like

this is a string


it comes out in the file like

this
is
a
string

any way .. can u guys tell me how i can make it so that it stores the input into string variable only when user presses enter and not space...

im uploading the whole project this time..



test.zip
 Description:

Download
 Filename:  test.zip
 Filesize:  5.78 KB
 Downloaded:  100 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Nov 11, 2006 11:00 pm   Post subject: (No subject)

When you use the extraction operator to obtain a string from an input stream, it stops at the first whitespace. To read an entire line, you must use the std::getline function.
bugzpodder




PostPosted: Sun Nov 12, 2006 10:25 am   Post subject: (No subject)

don't forget to becareful when mixing cin with getline. see post named random string line
wtd




PostPosted: Sun Nov 12, 2006 10:56 am   Post subject: (No subject)

Could you explain what you mean about mixing cin and getline? I don't think I'm seeing the issue you are.
halo3d




PostPosted: Sun Nov 12, 2006 12:22 pm   Post subject: (No subject)

so how exactly do i use std::getline in my program here? i mean whats the syntax and stuff... any example?
ericfourfour




PostPosted: Sun Nov 12, 2006 1:38 pm   Post subject: (No subject)

I would try
code:
input = std::getline ();
It wouldn't kill you to use Google every now and then.
wtd




PostPosted: Sun Nov 12, 2006 1:52 pm   Post subject: (No subject)

http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespacestd.html#a645
halo3d




PostPosted: Sun Nov 12, 2006 2:02 pm   Post subject: (No subject)

ericfourfour wrote:
I would try
code:
input = std::getline ();
It wouldn't kill you to use Google every now and then.


Why thanx ericfourfour i would use google from now rather than this site... thanx for the "little" help..

bye
Sponsor
Sponsor
Sponsor
sponsor
bugzpodder




PostPosted: Mon Nov 13, 2006 10:09 pm   Post subject: (No subject)

wtd wrote:
Could you explain what you mean about mixing cin and getline? I don't think I'm seeing the issue you are.


cin>>x misses the newline char. getline chews everything including the newline char. Hence a getline after a cin will usually result in an empty string being fetched, most likely not the intended behaviour. I don't see how one could get around this without managing newlines explicitly and very carefully.

Input
code:

56
this is a new line


code:

cin >> num;
getline(cin, str);
cout<<num<<endl<<str<<endl;


Output:
code:

56

wtd




PostPosted: Mon Nov 13, 2006 10:48 pm   Post subject: (No subject)

This just calls for input validation.

Maybe something like:

code:
struct non_empty_string
{
   std::string str;

   non_empty_string();
};

std::istream& operator>>(std::istream& in, non_empty_string& s);
std::ostream& operator<<(std::ostream& out, const non_empty_string& s)

int main()
{
   std::string x;
   non_empty_string nes;

   cin >> x >> nes;
   cout << x   << endl
        << nes << endl;   
}

non_empty_string::non_empty_string() : str("")
{
}

std::istream& operator>>(std::istream& in, non_empty_string& s)
{
   do  { std::getline(in, s.str); } while (s.str == "");

   return in;
}

std::ostream& operator<<(std::ostream& out, const non_empty_string& s)
{
   return out << s.str;
}
bugzpodder




PostPosted: Mon Nov 27, 2006 2:31 pm   Post subject: (No subject)

one more thing to be careful of: getline doesn't actually get the newline, so you must eat up the newline youself!
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: