0

In the switch function if you enter a letter or character the default case bugs out and is stuck in a infinite loop. Here is a mini version I created.

#include <iostream>
#include <string>
#include <cmath>
#include <unistd.h>
#include <chrono>
#include <thread>
#include <fstream>
#include <random>
#include <time.h>

using namespace std;



void sleepcp(int milliseconds) 
{
    clock_t time_end;
    time_end = clock() + milliseconds * CLOCKS_PER_SEC/1000;
    while (clock() < time_end)
    {
    }
}

int main() {
  START:
  int x;
  cout << "Enter a number: ";
  cin >> x;
  switch (x) {
    case 1:
      cout << "Hi"<< endl;
      sleepcp(250);
      system("clear");
      goto START;
    default:
      cout << "Incorrect"<< endl;
      sleepcp(250);
      system("clear");
      goto START;
  }
}

Now if you run this code and type a character such as m it will constantly show Incorrect and will not receive any user input.

  • 3
    If `cin >> x` fails because you put in a non-integer, `cin.fail()` will be `true`. This will cause all subsequent attempts to read from `cin` to also immediately fail, meaning you will reach that `default` case over and over again. – Nathan Pierson Jul 20 '21 at 23:05
  • `clock()` usually measures elapsed CPU time, which may be different from real time. Anyway, a busy loop is an extremely wasteful way to delay; use [`sleep_for`](https://en.cppreference.com/w/cpp/thread/sleep_for) instead. – Nate Eldredge Jul 20 '21 at 23:05
  • 2
    Also, please don't use `goto`. It has it's use cases, but you are unlikely to encounter them yet. You can replace using `goto` in this case with a while loop. – NathanOliver Jul 20 '21 at 23:05
  • I would suspect that the input buffer still contains the character... Where do you read it ? ... ;) – Otherwise, try to flush `cin`... – Tenphase Jul 20 '21 at 23:07
  • 1
    If `operator>>` fails to read in the requested type, it sets the `failbit` flag on the stream. You need to `clear()` that flag before you can read from the stream again. And then you can `ignore()` the bad input, if needed. Also, you REALLY need to stop using `goto` as I mentioned in [my answer](https://stackoverflow.com/a/68460472/65863) to your [previous question](https://stackoverflow.com/questions/68460392/). In this case, a `do..while` loop is more appropriate. – Remy Lebeau Jul 20 '21 at 23:10
  • @Tenphase A side note: it is harder to reliably flush `cin` than it sounds. – user4581301 Jul 21 '21 at 18:49
  • @user4581301 I was about to answer: _"Something like: `cin.ignore(INT_MAX)`"_. But what you said made wonder: _"Why would it be harder than that ?"_... – It took me less than two minutes to find a better answer, and thus, I learned something new... _(thanks ;)_ – A better way to do it would be doing `cin.clear()` _(for eventual errors)_ and then `cin.ignore(std::numeric_limits::max())` using the optional argument if desired... _more info [here](https://stackoverflow.com/a/257182/16120728)_ – What I learned is that `streamsize` is not necessarily an `int`... ;) – Tenphase Jul 22 '21 at 07:01
  • @Tenphase That's different than what I was expecting. Most folks try to `fflush(stdin);` a non-Standard extension that only works on some systems. – user4581301 Jul 22 '21 at 15:32
  • @user4581301 `fflush(stdin);` is a `C` construct using file descriptors. It should be valid on every system claiming to implement the `C` standard _(although the file descriptors are platform-dependent)_... – It is not a `C++` function... `stdin` is not `cin`and is not defined by the STL _(one has to include cstdio, which is stdio.h)_ – It is possible, though, to _"map/bind"_ `cin` to `stdin` and eventually use `fflush`, but I don't remember how exactly now, and, anyway, that's something I wouldn't recommend to do _(unless there are good reasons to do so, and no other workaround)_ ... _;)_ – Tenphase Jul 22 '21 at 18:55
  • 1
    Not quite correct. `ffflush`'s behaviour is not defined when fed an input stream. Some implementations, notably POSIX, clear the stream, but officially you shouldn't try it. – user4581301 Jul 22 '21 at 19:11
  • @user4581301 Hmm, I think you're damn right ! It reminds me of something now... Or rather, it reminds me why I don't recall exactly how to _"bind/map"_ `cin` to `stdin`, but I remember, on the other hand, that _"it is not a good idea to do so unless there are good reasons to do so, and no other workaround"_ ... – To which I should have had added _(as I do now)_: "I cannot think to a situation where one could not do otherwise, because there is always a better solution". _(thanks for the correction and the reminder)_ – Tenphase Jul 22 '21 at 19:34
  • I think you're trying to remember [freopen](https://en.cppreference.com/w/c/io/freopen). I haven't found a good use-case for redirecting cin either, though. I usually write functions that take an `istream` and then I can pass `cin`, a `ifstream`, or any other input stream. – user4581301 Jul 22 '21 at 20:11

0 Answers0