
-----------------------------------
halo3d
Sat Nov 11, 2006 10:45 pm

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..

-----------------------------------
wtd
Sat Nov 11, 2006 11:00 pm


-----------------------------------
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
Sun Nov 12, 2006 10:25 am


-----------------------------------
don't forget to becareful when mixing cin with getline.  see post named random string line

-----------------------------------
wtd
Sun Nov 12, 2006 10:56 am


-----------------------------------
Could you explain what you mean about mixing cin and getline?  I don't think I'm seeing the issue you are.

-----------------------------------
halo3d
Sun Nov 12, 2006 12:22 pm


-----------------------------------
so how exactly do i use std::getline in my program here? i mean whats the syntax and stuff... any example?

-----------------------------------
ericfourfour
Sun Nov 12, 2006 1:38 pm


-----------------------------------
I would try input = std::getline ();It wouldn't kill you to use Google every now and then.

-----------------------------------
wtd
Sun Nov 12, 2006 1:52 pm


-----------------------------------
http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespacestd.html#a645

-----------------------------------
halo3d
Sun Nov 12, 2006 2:02 pm


-----------------------------------
I would try 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

-----------------------------------
bugzpodder
Mon Nov 13, 2006 10:09 pm


-----------------------------------
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

56
this is a new line



cin >> num;
getline(cin, str);
cout