0

I have an extract from my code that is supposed to check wheter or not the input given by the user is an integer. If so, the code should go through, if not, the program should jump back/rebegin at a defined point.

    again1: // Inloop point 1

    cout << "   " + player1 + ", where would you like to place a chip? Enter the index (1 - 9).\n" << endl;
    int index1; // Initializing index 1
    cin >> index1; // Writing input of player 1 into index 1

    if (cin.fail()) {
        cout << "Error" << endl;
        goto again1;
    }

Therefore, I would expect my program to ask me again to enter an index if I had tried to give an invalid one before. But if I type e.g. "a" (which is invalid because it's a char), the terminal window just runs my message "Error" hundreds of times in an endless loop and does not jump back to again1.

PS: The goto principle itself works. I have implemented it several other times in the same .cpp file and it works well.

Any tips?

  • *I have implemented it several other times in the same .cpp file and it works well.* -- It may work, but whoa be on the person who has to change the code. As to the issue, you should read into a string and inspect to see if it is a valid integer. – PaulMcKenzie Oct 21 '21 at 12:51
  • "The goto principle itself works." if it does, what are you doing here? – Slava Oct 21 '21 at 12:52
  • [here](https://stackoverflow.com/a/33857552/509868) – anatolyg Oct 21 '21 at 12:52
  • @Slava I meant that the reason for this strange terminal behavior does not lay in the usage of this construction itself (at least I think so) but maybe in some other logical mistake... –  Oct 21 '21 at 12:54
  • 1
    `if (std::cin >> value) { process(value); .... ` – Marek R Oct 21 '21 at 12:57
  • @Marek R Thanks, that was exactly what I needed. No condescending talk, no blubber, just a simple comment. Awesome! :) –  Oct 21 '21 at 13:03
  • 3
    With the `cin.fail()` condition, you'll need to `cin.clear();` to clear the failure condition, and be aware that the "bad input" is still pending in the stream so you might need to clear that "bad" line by `cin.ignore(std::numeric_limits::max(), '\n');`. – Eljay Oct 21 '21 at 13:25
  • @Eljay Thanks, works fine! Great advise :) –  Oct 21 '21 at 13:44
  • Re: "if I type e.g. "a" (which is invalid because it's a char)" -- anything you type is a char. The key here is that `'a'` is not a **digit**, while `'1'` is. Console input is **always** characters, and it's up to the program to determine whether those characters can be interpreted in a way that makes sense. You can use the built-in character conversions, with `int x; std::cin >> x;`, or you can read plain text and interpret it yourself, with something like `std::string in; std::readliine(std::cin, in); int x = std::stoi(in);`. – Pete Becker Oct 21 '21 at 14:18
  • @SvenEschlbeck I meant that it is not recommended to use `goto` in your program, not because "it does not work" but because it produces difficult to read and understand program code. And you ask us to read it, because there is an issue. – Slava Oct 22 '21 at 13:13
  • @SvenEschlbeck -- To add to what Slava mentioned, programs that have an issue and require an extra set of eyes to look at it, usage of `goto` basically limits the amount of help you will get with such a program, and in extreme cases, no help will be given at all (advice would be given to have the program better structured). The issue is that a programmer is not going to waste time trying to untangle a potential mess with programs that have `goto`'s in it. – PaulMcKenzie Oct 22 '21 at 20:50

0 Answers0