Help with cin / input
Jan 23, 2016 at 10:43pm UTC
I am writing a program that loops until the user enters in anything other than a 'Y' or 'y' but even after inputting a 'y' the loop breaks instead of continuing. I have no idea why.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
int main()
{
bool play = true ;
int count = 0;
char input;
vector<double > high_score_vector;
while (play == true ) {
double score = the_game();
high_score_vector.push_back(score);
cout << "Do you want to play again? (Y/N)" << endl;
cin >> input;
while (!cin.good()) {
cin.clear();
cin.ignore(10000, '\n' );
cout << "Not valid input! Try again " << endl;
cin >> input;
}
if (input != 'Y' || input != 'y' )
play = false ;
count++;
}
output(high_score_vector, count);
return 0;
}
Jan 23, 2016 at 11:05pm UTC
This condition is always true:
if (input != 'Y' || input != 'y' )
When testing not-equal, use && rather than || to combine multiple conditions.
Topic archived. No new replies allowed.