Accepting user input in a while loop

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


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


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

the 'or' version is always true


  • How to shorten this expression using regex
  • Grails eager fetch doesn't retrieve all data
  • Sql indexes vs full table scan
  • C# Remove URL from String
  • C++ Coin flip program error
  • string.IsNullOrEmpty() Doesn't Seem to Work on a String within a Class within a Class
  • Updating and removing unique join relationships in CakePHP
  • Entity Framework ObjectContext: Concurrency
  • Want to understand iframe breakout code
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • .NET video play library which allows to change the playback rate?
  • Building Qt project for C++11 standard
  • C++ friend class std::vector
  • WPF Template Binding in ToggleButton UserControl
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • How to 'create temp table as select' in Slick?
  • Android changing fragment order inside FragmentPagerAdapter
  • Mysql - How to search for 26 records that each begins with the letter of the alphabet?
  • CakePHP ACL tutorial initDB function warnings
  • MySQL Order by column = x, column asc?
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Python CGI os.system causing malformed header
  • Using variable in a value field in jMeter
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Regex thinks I'm nesting, but I'm not
  • what is the difference between the asp.net mvc application and asp.net web application
  • Weird JavaScript statement, what does it mean?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Jquery - Jquery Wysiwyg return html as a string
  • Numpy divide by zero. Why?
  • Confusion with PayPal's monthly billing cycle
  • Matrix multiplication with MKL
  • Checking variable from a different class in C#
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs