I am currently designing a program for my class in Programming Fundamentals I.
The teacher is asking me to do a while loop to continue the program, but circulate a new session after the user decided to end the session. I already have designed a do while loop for this purpose so that it can continue to pre-evaluate what's necessary.
My problem doesn't start with the do while loop. It starts with the part of trying to enter a value after the end user has entered an invalid one. I will highlight the bold to show you the problem:
Here's the code:
do {
cout << "\nPlease choose your method of conversion: " << endl;
cout << "1 - C to F" << endl;
cout << "2 - F to C" << endl;
cout << "Enter a number: ";
cin >> userInput;
cout << endl;
if (userInput == 1)
{
cout << "The formula to convert Celsius to Fahrenheit is " << endl;
cout << "F = (C * 9 / 5) + 32.\n" << endl;
cout << "Enter a value to convert: ";
cin >> C;
if (C)
{
F = (C * 9 / 5) + 32;
cout << setprecision(1) << fixed << showpoint;
cout << "\nYou entered " << C << " and converted it to " << F << endl;
}
**else if (!C)
{
cout << "Please enter a valid value to convert.";
}**
}
else if (userInput == 2)
{
cout << "The formula to convert Fahrenheit to Celsius is " << endl;
cout << "C = (F -32) * 5 / 9.\n" << endl;
cout << "Enter a value to convert: ";
if (F)
{
cin >> F;
C = (F - 32) * 5 / 9;
cout << setprecision(1) << fixed << showpoint;
cout << "\nYou entered " << F << " and converted it to " << C << endl;
}
**else if (!F)
{
cout << "Please enter a valid value to convert.";
}**
}
cout << "Do you want to convert again? (Y/N): ";
cin >> endSession;
} while (endSession == 'Y');
What I want to do for else if !(C or F) is to make sure that I can input the correct value, return the value, then calculate. I want to make sure that even if the end user made errors in computing the correct value even after the first time, the end user will continue the program until a correct value is entered. Once that's entered, the do while loop then ends the session if the user wants to convert again.
I have tried either while inside the nested if else ifs; C&P'd the conversion value into the else if !(), producing a continuous error non-stop. Even though I don't ask for the straight answer, some helpful hints or pointing in the right direction would literally help. In fact, it would help if it was basic.