Cin >> does not work for two words ---Please Help

Cin >> does not work for two words ---Please Help

Page 1 of 1

2 Replies - 5089 Views - Last Post: 01 March 2008 - 06:15 AM Rate Topic: -----

#1 SEOT   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 01-December 07

Cin >> does not work for two words ---Please Help

Posted 29 February 2008 - 08:27 PM

I was using cin to get one word in my program when a user enters from a keyboard. Now, I need to get two words from a user inputing from a keyboard, but the cin >> statement does not work? Any ideas.....please help me - CTYD

View PostSEOT, on 29 Feb, 2008 - 08:23 PM, said:

I was using cin to get one word in my program when a user enters from a keyboard. Now, I need to get two words from a user inputing from a keyboard, but the cin >> statement does not work? Any ideas.....please help me - CTYD



I found it!!! You put cin.ignore() before a getline, and it works. This was buried on the internet. How are you supposed to find this type of thing???? Problem Solved-- SEOT

Is This A Good Question/Topic? 0
  • +

Replies To: Cin >> does not work for two words ---Please Help

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Cin >> does not work for two words ---Please Help

Posted 01 March 2008 - 12:54 AM

Although not technically a *bug* the fact that cin leaves unused characters in the buffer is at best irritating. To be sure that you clear all of the extra garbage left in the buffer you should use something like:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');


The other problem can be that the last input was an error (this often occurs if you asked the user for a number and they entered a character etc.), in this case you want to use:
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Was This Post Helpful? 0
  • +
  • -

#3 letthecolorsrumble   User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Cin >> does not work for two words ---Please Help

Posted 01 March 2008 - 06:15 AM

Also check out a similar question posted in this topic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1