I am writing an error message for my code using a while loop.
The user has a choice between symbols % or $.
If entered an incorrect symbol, error message appears.
My while loop terminates after entering % or $, but so does the rest of my program.
Cannot figure out what is going on.
//QUESTION
char choice;
cout << "Would you like to give the down payment as a dollar amount($) or percent(%)" << endl;
cout << "Enter either symbol: % or $ " << endl;
cin >> choice;
//ERROR MESSAGE LOOP
while(choice != '$' || choice != '%') {
cout << "ERROR: Try Again, invalid input! Enter either symbol: % or $"<<endl;
cin>>choice;
if( choice == '%' || choice == '$') {
return choice;
}
}
>Solution :
My while loop terminates after entering % or $, but so does the rest
of my program.
That’s what a return statement does, it terminates the entire function, not just the loop. You need to change it to a break statement (and do something useful with the input before the break depending on the logic that you need).