help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why doesn't code from TYC++in21days work?


From: Guy Harrison
Subject: Re: Why doesn't code from TYC++in21days work?
Date: Mon, 09 Aug 2004 01:00:01 GMT
User-agent: KNode/0.7.7

Gerald Lafreniere wrote:

> Now that I have it working, there is a loop bug happening.
> 
> I compiled it in Win2K with mingw 3.2 & Dev-Cpp
> 
> If you enter one of the selections, then you're okay.  If you enter
> something else,
> the program enters an infinite loop, gliding over the 101:cin >> choice;

Don't panic.
 
> I am just wondering where the problem is.  I am guessing it's the OS and
> its header stuffs.

Nope. Your initial problem has (naturally) mislead you. It is standard C++
behaviour for a stream to fail on bad input. 'choice' is an 'int' and if
the input can't be converted to an 'int' the stream (loosely) "locks". It
has to be clear()'ed and the bogus input flushed out.

> Does this happen with your compiler?  Is there a work-around?

Yes & yes. You need to ask in alt.comp.lang.learn.c-c++ now though. I can't
explain here what the hint below does. The folks over there will though.

<hint>
if (!(cin >> choice)) {
  cin.clear();
  while ('\n' != cin.get())
  ;
  return -1;
}
return choice;
</hint>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]