Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ While-Loop (loop terminates along with the rest of my program)

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;
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading