0

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.

  • By the way, beware [integer division](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division-in-c) `F = (C * 9 / 5) + 32;`, the expression `9 / 5` will result in `1`, similarly `5 / 9 == 0`. – Cory Kramer Feb 20 '16 at 20:53
  • Minor point: if you have already checked `if (C) {…}`, then `else if (!C) {…}` can simply be written `else {…}` – jtbandes Feb 20 '16 at 20:53
  • @CoryKramer `a*b/c` means `(a*b)/c`, not `a*(b/c)`. –  Feb 20 '16 at 21:01
  • @hvd Correct, but regardless it's not going to result in what the OP is likely expecting – Cory Kramer Feb 20 '16 at 21:06
  • @CoryKramer For suitable types for `C` and `F`, it will do the right thing. –  Feb 20 '16 at 21:10

0 Answers0