0

I've been coding in C++ for all of a few days, so please talk to me like I'm a baby. With that out of the way,

I've made a short algorithm that asks a set of questions (to be answered 0 for no and 1 for yes) and stores the user's answer as an integer. Everything works as expected as long as the user only inputs integers (or, in one case, a string with no spaces).

But if the input doesn't match the variable type, the program immediately outputs this infinite loop that appears later in the program. That loop is supposed to print a question, wait for input, and then ask again if the answer isn't '1', but in the failure state it just prints the question without end. I can't see any reason why the previous questions would be connected to this. It doesn't happen on the questions that come after it, if that's a clue.

Here's a pared-down version of my code with, I hope, all the important information intact:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int answer1;
    string answer1C;
    int answer2;
    int answer2B;
    int score;
    score = 0;

    cout << "Question 1" << endl;
    cin >> answer1;
    if (answer1 == 1)
    {
        ++score;
        //some other questions go here
        cout << "Question 1C" << endl;
        cin >> answer1C;
        if (answer1C.size() == 6)
        {
            ++score;
        }
    }

    cout << "Question 2" << endl;
    cin >> answer2;
    if (answer2 == 0)
    {
        cout << "Question 2B" << endl;
        cin >> answer2B;
        if (answer2B == 0)
        {
            --score;
        }
        while (answer2B != 1) //Here is the infinite loop.
        {
            cout << "Question 2B" << endl;
            cin >> answer2B;
        }
    }

    cout << "Question 3" << endl;
    //and so on

    return 0;
}

I would love to have it accept any input and only perform the ensuing steps if it happens to meet the specified conditions: for instance, in question 1.2, it only awards a point if the answer is a string of length 6, and otherwise does nothing; or in question 2.1, it repeats the question for any input that isn't '1', and moves on if it is.

But in any case whatsoever, I need it to do something else when it fails. Please help me figure out why this is happening. Thank you.

  • A similar answer was provided here that looks at the validations for `cin` you can employ to handle either numeric or character data [how to convert int to char, char to int after get an input](https://stackoverflow.com/questions/54396835/how-to-convert-int-to-char-char-to-int-after-get-an-input/54398368#54398368). For details of stream states see [std::ios_base::iostate](https://en.cppreference.com/w/cpp/io/ios_base/iostate) – David C. Rankin Jan 29 '19 at 23:50
  • The reason for the scrolling repeated loop is because the "bad" data is still in the stream. You need to (a) clear the bad state of the stream, and (b) consume and discard the bad data, likely through some console input such as a newline marker. – WhozCraig Jan 29 '19 at 23:55
  • WhozCraig, I have no idea how to do that. – the-baby-is-you Jan 29 '19 at 23:56
  • 1
    `cin.ignore(numeric_limits::max(), '\n');` will discard all remaining characters in `stdin`, include `limits`. `cin.clear();` clears `failbit`. See also [cin input (input is an int) when I input a letter,...](https://stackoverflow.com/questions/50402749/cin-input-input-is-an-int-when-i-input-a-letter-instead-of-printing-back-inco/50403158?r=SearchResults&s=1|19.4078#50403158) – David C. Rankin Jan 29 '19 at 23:58
  • What does that mean, where do I put it, what's a stdin? I told you, I'm a baby. – the-baby-is-you Jan 30 '19 at 00:04
  • I added an additional link to the end of the last comment that shows an example of its use -- doing what you need. – David C. Rankin Jan 30 '19 at 00:04
  • I think this answer will solve your problem. https://stackoverflow.com/a/27004240/3807729 – Galik Jan 30 '19 at 00:16

0 Answers0