Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hi friends im facing one problem
i have used cin to input a choice like (cin>>ch)
then i have used to take input to string as getline(cin, string_s)
but getline does not works when i use cin

if i donot use cin it works well
why it is happening???
and is there any alternative to take integer then string input
Posted
Comments
Richard MacCutchan 25-Apr-11 5:20am    
Please post the exact code that does not work and explain what happens.

getline() reads input until the newline character and cin>> fails to remove the newline character after reading input. By calling getline() after cin>>, you're essentially just grabbing the newline character (and any white space) that cin>> left behind.

http://www.cplusplus.com/reference/string/getline/[^]

You can use getline() to get the number as well (just have to take an extra step to interpret as number).
 
Share this answer
 
v3
Comments
Niklas L 26-Apr-11 4:11am    
Very likely cause. 5.
This is what I have and it works perfectly -
using namespace std;
string str;
getline(cin, str);
cout << str << endl;
 
Share this answer
 
Comments
Albert Holguin 25-Apr-11 10:37am    
Don't think you understood the OPs problem, see my solution.
«_Superman_» 25-Apr-11 22:09pm    
I think you're correct.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900