Error check issue

I have been having trouble entering an error check to this code for when a user inputs a letter instead of numbers for the Celsius or Fahrenheit input

#include <iostream>
#include <iomanip>

using namespace std;

int main()

{
float Celsius = 0; //variable for Celsius
float Fahrenheit = 0; //variable for Fahrenheit
char Selection = 0; //variable for menu selection
char Again = 0; //variable to repeat menu
char Exit = 0; //confirm quit

cout << setprecision(2) << fixed << showpoint;
// setting precision for accurate display

cout << "This program will assist in converting degrees up to the" << endl;
cout << "hundredths decimal value" << endl;
// informing the user of what the program is used for and what format to use
cout << endl;

do
{
cout << "Please select one of the following options:" << endl;
cout << endl;
cout << setw(35) << "1. Convert Celsius to Fahrenheit" << endl;
cout << setw(35) << "2. Convert Fahrenheit to Celsius" << endl;
cout << setw(10) << "3. Quit" << endl; //menu selection
cout << endl;
cin >> Selection;
cout << endl;

switch( Selection ) // This is where the switch statement begins
{
case '1': // Calculates C to F
{
cout << "Please input the beginning degrees in Celsius" << endl;
cout << endl;
cin >> Celsius;
Fahrenheit = ( 9.00 / 5 ) * Celsius + 32 ;
cout << endl;
cout << "The calculate degrees to Fahrenheit is: " << Fahrenheit << endl;
cout << endl;
cout << "Would you like to start over? (Y/N)" << endl; //Restart option
cout << endl;
cin >> Again;
cout << endl;
}
break;

case '2': // Calculates F to C
{
cout << "Please input the beginning degrees in Fahrenheit" << endl;
cout << endl;
cin >> Fahrenheit;
Celsius = ( 5.00 / 9 ) * (Fahrenheit - 32) ;
cout << endl;
cout << "The calculate degrees to Celsius is: " << Celsius << endl;
cout << endl;
cout << "Would you like to start over? (Y/N)" << endl; //Restart option
cout << endl;
cin >> Again;
cout << endl;
}
break;

case '3': // Quit program
{
cout << "Would you like to start over? (Y/N)" << endl; //Restart option
cout << endl;
cin >> Again;
cout << endl;
}

break;

default:
{
cout << "You did not enter a valid option" << endl; // error check
cout << endl;
cout << "Would you like to start over? (Y/N)" << endl; //Restart option
cout << endl;
cin >> Again;
cout << endl;
}
}
}
while ( Again == 'Y' || Again == 'y' );

cout << "Thank you. Good bye" << endl; // end message
cout << endl;

return 0;
}
You forgot to include a break at the end of the default case.

Edit: I take that back. It doesn't matter.
Last edited on
Any suggestions on how to error check the input for the Celsius or Fahrenheit values so that it won't register a letter
if a user enters a letter when it's expecting a number. The iostream is going to enter an "error state mode".

I'll be honest. This is a little more work. Not a ton, but you will need to write more code.

Check for cin.fail(). Then call cin.clear() and cin.fail()

http://www.cplusplus.com/forum/beginner/26821/

cin.fail() is a way of checking if you are in an error state mode.

Edit: I apologize for the first response. I honestly wasn't sure what was being asked.
Last edited on
Topic archived. No new replies allowed.