Calculator. How to fix if user types letters isto numbers

Pages: 12
IMO, for interactive console work it doesn't make any difference. The only time it does matter is when writing to files (including pipes etc) when \n should always be used which shouldn't cause a stream flush. Or if stdout is re-directed away from the console screen.

PS Again IMO, when endl was being discussed for inclusion into C++ it should have been aborted and never put in. For anything other than interactive console work it causes many problems and doesn't really solve any! If you really want a stream flush at some point, you can always use flush(). Note that stream close (implicit at object destruction or explicit) does an implied flush.
Last edited on
When considering whether to retry, first check that it's actually possible to recover.

It's only possible to recover if the input stream is in a recoverable error state. Usually that means the stream's error mask must be exactly std::ios_base::failbit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <limits>

std::istream& ignore_field(std::istream& is)
{
    return is.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
}

int main()
{
  int n;

  do if (std::cin >> n)
      std::cout << n << '\n'; 
    else if (std::cin.rdstate() == std::ios_base::failbit) {
      std::cin.clear(); 
      ignore_field(std::cin);
    }
  while (std::cin);
}


This is so painful because C++ streams are not designed for user-interaction.

IRL we may also want to check that std::cin is actually connected to a terminal device.
Last edited on
Better check the power is on too.
This is so painful because C++ streams are not designed for user-interaction.


We know! But the chance of the keyboard input stream not being in a recoverable error state is extremely small - so for the purpose of this original question really just complicated matters further.

There's also the possibility that cin isn't the keyboard but a file if stdin has been redirected etc etc. But for some simple code for the OP reasonable question.....
There's also the possibility that cin isn't the keyboard but a file if stdin has been redirected etc etc. But for some simple code for the OP reasonable question.....
Maybe it's not true for OP's case, but most console programs are used non-interactively. In these cases, retrying is futile.

We know!
You know, but OP probably doesn't.

The chance of the keyboard input stream not being in a recoverable error state is extremely small - so for the purpose of this original question really just complicated matters further.
Even if we assume interactive usage, the user may type Ctrl-D (Ctrl-Z on Windows) to instruct his console to stop sending input to the job's standard input.

This causes an infinite loop because every read will fail. No amount of resetting error state flags will fix this issue.
Last edited on
Well that settles it. Best to just buy a new keyboard straight away if somebody types a letter into the Stream of Doom. Especially if its a mac because there’s no Ctrl key anyway.
How to fix if user types letters into numbers


See keyinp.hpp www.cplusplus.com/forum/general/273018/

for functions that obtain different types of keyboard input with checking etc
Last edited on
Topic archived. No new replies allowed.
Pages: 12