0

I have just started learning C++ and trying to learn the syntax.

#include <iostream>
#include <limits>
using namespace std;
int main(){
    bool answer;
    cout << "Did you enjoy testing this program? (1 for yes, 0 for no) ";
    cin >> answer;
    while (!(cin >> answer)) {
        cout << "Invalid value!\n";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Please type either 0 or 1: ";
        cin >> answer;
    }
    cout << "Your feedback has been registered. Feedback: " << answer;
}

The aim is to keep making the user ask over and over until they input either 0 or 1. The code snippet just makes things freeze when either of those values is given. How should this be fixed?

3
  • 1
    You're waiting for the user to enter the value twice for every one time you are prompting them. Commented Sep 15, 2022 at 21:03
  • It might he worth pointing out that requiring a human to be present is almost always the wrong thing to do. Bad input should mean immediate termination with error message and error code. Commented Sep 15, 2022 at 22:31
  • @Dúthomhas: I agree that especially on POSIX-based operating systems, programs should generally be designed to accept input not only from humans, but also from other programs. However, I see nothing wrong with a program also having an interactive mode which is designed for human input and which allows a human to correct his mistakes by retyping input. Commented Sep 15, 2022 at 23:59

1 Answer 1

4

The cin >> answer; statement above the loop, and the cin >> answer; statement at the end of the loop body, both need to be removed.

You are prompting the user to enter a value, then you read in that value and ignore it, and then you wait for the user to enter in another value, even though you didn't prompt the user to enter more than 1 value.

If they do happen to enter a 2nd value, and it fails, your loop will then prompt the user to enter in a new value, then you read in that value and ignore it, and then you wait for the user to enter yet another value without prompting the user to do so.

You should be invoking cin >> answer only 1 time per loop iteration, eg:

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

int main(){
    bool answer;
    cout << "Did you enjoy testing this program? (1 for yes, 0 for no) ";
    // cin >> answer; // <- remove this!
    while (!(cin >> answer)) {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid value!\n";
        cout << "Please type either 0 or 1: ";
        // cin >> answer; // <- remove this!
    }
    cout << "Your feedback has been registered. Feedback: " << answer;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.