Simon Gibson wrote:
[color=blue]
> "John Harrison" <john_andronicus@hotmail.com> wrote in message
> news:c1kqn8$1jkhbu$1@ID-196037.news.uni-berlin.de...
>[color=green][color=darkred]
>>>This question is covered in the FAQ
>>>
>>>
http://www.parashift.com/c++-faq-lit....html#faq-15.6
>>>
>>>john
>>>[/color]
>>
>>I think the FAQ is less than helpful on this question, here's exactly what
>>happens.
>>
>>Your program asks for a menu option and the user types
>>
>>2 <newline>
>>
>>Then your program asks for a report and the user types
>>
>>this is my report <newline>
>>
>>At this point cin.getline() starts reading characters. Now at this point[/color]
>
> how
>[color=green]
>>many newlines have been read so far? You think the answer is one and so[/color]
>
> you
>[color=green]
>>can't understand why getline seems to skip the report. But the answer is
>>zero, because cin >> i NEVER reads newlines (assuming i is a number). The
>>newline that was typed after the menu option is still waiting to be read[/color]
>
> and
>[color=green]
>>getline reads it and stops.
>>
>>This is why, after interactive line based input its a good idea to add
>>
>>cin.ignore(INT_MAX);
>>
>>after the input of a number, to clear out any unread newlines. Use the
>>header file <limits.h> to get the INT_MAX constant.
>>
>>john
>>
>>[/color]
> Cheers for that, one thing tho, i tried using the cin.ignore(INT_MAX); but
> when i entered the number 2 to go into the enter match bit it didnt come up
> it just kept letting me press enter with nothing happening. i tried using
> cin.ignore() and it works fine, could you explain the INT_MAX bit? im only
> just learning so any info is appreciated.
> cheers
> si[/color]
1. Don't top-post. Replies go at the bottom or are interspersed.
2. I believe the ignore syntax should be:
cin.ignore(INT_MAX, '\n');
This says to ignore either INT_MAX characters or '\n', which
ever occurs first.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book