Akula Akula - 19 days ago 6
C++ Question

C++: Having troubles with do while loop

do {
cout << "Enter the account type (C for current and S for savings): ";
cin >> account_type;
} while (account_type != 'S' || 'C');


I have account_type set as char,the problem is that everytime i run the program and i input S or C the loop keeps repeating.Can anyone help me know why its happening?

Answer

All non-zero values in c++ evaluate to true when used in a boolean operation. So account_type != 'S' || 'C' is the same as account_type != 'S' || true. Which means your loop never exits.

What you need to do is to preform both checks

do {
    cout << "Enter the account type (C for current and S for savings): ";
    cin >> account_type;
} while (account_type != 'S' && account_type != 'C');
Comments