getline after cin>>

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
nukeu666
New poster
Posts: 44
Joined: Sun Feb 13, 2005 1:13 am
Location: India
Contact:

getline after cin>>

Post by nukeu666 »

what exactly am i supposed to pass to cin.ignore if i need to use cin.getline after using a cin>> previously?
one methos i read about uses limits...does OJ support including limits?
google
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

Hmmm... I have not used cin.ignore before. May I suggest an alternative? How about using two getlines? You can also use getline this way:

getline(cin,str);

where str is a C++ string.

One to read in the newline after the cin >> and the second one to read in the line you want.
ghst08
New poster
Posts: 1
Joined: Mon Jun 11, 2007 2:44 am

Post by ghst08 »

you can use cin.get(source, len) to get the string and then use
cin.ignore(INT_MAX, '/n')

that way itll clear out the buffer left up to the amount of INT_MAX or till it finds a null terminator.
Zyaad Jaunnoo
Experienced poster
Posts: 122
Joined: Tue Apr 16, 2002 10:07 am

Re: getline after cin>>

Post by Zyaad Jaunnoo »

nukeu666 wrote:what exactly am i supposed to pass to cin.ignore if i need to use cin.getline after using a cin>> previously?
one methos i read about uses limits...does OJ support including limits?

Code: Select all

Use cin.get() to swallow the carriage return.
Then use cin.getline(..)
Post Reply

Return to “C++”