vendredi 4 décembre 2015

Cin making me enter twice

I have the code below to trap the users. The problem is when the while is triggered because of the user entering in a character or string, it makes the user enter the data twice before accepting it. I'm not quite sure why it's doing that and was hoping someone could explain it to me. thanks!

    bool valid_entry = false;
while (!valid_entry) {
    cin >> distanceTraveled;
    while ((!(cin >> distanceTraveled)) ||(distanceTraveled==0)) {
        cin.clear();
        cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
        cout << "ERROR:  invalid Entry.   Try again.\n\nHow far did ya go? ";
        cin.clear();
        cin >> distanceTraveled;
    }

        valid_entry = true;

}

Aucun commentaire:

Enregistrer un commentaire