Zheis Viscia
Zheis Viscia

Reputation: 27

Having trouble with an if statement. (New programmer.)

    correct = 0;
    cout << "You entered " << years << ".\nIs that correct? (Y/N) \n";
    cin >> yesNo;
    if (yesNo == 'y' || 'Y')
         correct ++;
} while (correct != 1);
    cout << "good! " << correct << endl;
return 0;

} I want it to only ++ the correct int if yesNo = y or Y, if it doesn't, I want it to loop back up to the top (unseen)

Upvotes: 1

Views: 65

Answers (2)

Post Self
Post Self

Reputation: 1563

http://coliru.stacked-crooked.com/a/5bf3edba51feec45

Your problem is the if (yesNo == 'y' || 'Y'). What this if-statement says, is

If yesNo is equal to 'y' or 'Y' is true, proceed.

The thing is, 'Y' is always true, because the char is implicitly converted into a bool.

What you probably meant, is if (yesNo == 'y' || yesNo == 'Y')

Upvotes: 3

CodeZero
CodeZero

Reputation: 1699

I guess your problem is in

if (yesNo == 'y' || 'Y')

It should be

if (yesNo == 'y' || yesNo== 'Y')

Upvotes: 6

Related Questions