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 » Fri Jun 23, 2006 9:39 pm

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 » Sat Jun 24, 2006 11:54 am

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 » Mon Jun 11, 2007 2:48 am

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 » Wed Oct 29, 2014 6:29 pm

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++”