4

I'm trying to create a simple temperature conversion program that allows the user to convert temperatures as many times as they want until they decide to end the program by entering the letter 'e'. Everything else in the code works except the part where the user enters the letter 'e'. If I take the last else statement out, the program just starts at the beginning of the loop again. If I leave the else statement in, when the user enters the letter 'e', the else statement thinks that it is invalid input and does not end the program.

 #include <iostream>

using namespace std;

float celsiusConversion(float cel){     // Calculate celsius conversion
    float f;

    f = cel * 9/5 + 32;
    return f;
}

float fahrenheitConversion(float fah){  // Calculate fahrenheit conversion
    float c;

    c = (fah - 32) * 5/9;
    return c;

}
int main()
{
    char userInput;

        while (userInput != 'e' or userInput != 'E')    // Loop until user enters the letter e
        {

            cout << "Please press c to convert from Celsius or f to convert from Fahrenheit. Press e to end program." << endl;
            cin >> userInput;


            if (userInput == 'c' or userInput == 'C') // Preform celsius calculation based on user input 
                {
                    float cel;
                    cout << "Please enter the Celsius temperature" << endl;
                    cin >> cel;
                    cout << cel << " Celsius is " << celsiusConversion(cel) <<  " fahrenheit" << endl;
                }
            else if (userInput == 'f' or userInput == 'F')  // Preform fahrenheit calculation based on user input
                {
                    float fah;
                    cout << "Please enter the Fahrenheit temperature" << endl;
                    cin >> fah;
                    cout << fah << " Fahrenheit is " << fahrenheitConversion(fah) << " celsius" << endl;
                }
            else    // If user input is neither c or f or e, end program
                {
                    cout << "Invalid entry. Please press c to convert from Celsius, f to convert from Fahrenheit, or e to end program." << endl;
                }
        }
    return 0;
}
2
  • You can't respond to individual key presses with only the standard library. You'll at least need to read an entire line of input. And for that there are thousands of duplicates already on SO. Commented Nov 12, 2012 at 17:54
  • 1
    This might make more sense as a do-while loop. Commented Nov 12, 2012 at 17:56

2 Answers 2

2

you mean while(userInput != 'e' && userInput != 'E')

the 'or' version is always true

Sign up to request clarification or add additional context in comments.

1 Comment

so that did end the program, but not before printing out the else line. any advice on how to fix that? EDIT: Never mind, I just changed the last else statement to: else if (userInput != 'e')
1

I'd make 2 changes in your code:

while (userInput != 'e' && userInput != 'E')

And, just before the "else" (to prevent the error message when exiting the program):

else if (userInput == 'e' or userInput == 'E')
             {
                break;
             }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.