This is one of my first lab assignments. I was to create a very simple program to asks for the temperature in celsius which the program would convert to fahrenheit, and then vice versa. With the option to either go again or exit the program.
I managed to do that successfully, but I was also instructed to put in a code to keep the program from crashing if the user typed a character instead of a integer. I been trying for days to use all kinds of codes or hints I found online but I just can't seem to get it right and working. Below is my code, any help or what to do or where I can get the information needed to complete this, would be appreciated.
#include <iostream>
int main()
{
int number ;
while( std::cout << "please enter a number: " )
{
if( std::cin >> number ) break ; // a number was entered; exit the loop
std::cin.clear() ; // clear the failed state
std::cin.ignore( 1000, '\n' ) ; // throw away the non-numeric input
std::cout << "error in input, please try again\n" ;
}
std::cout << "you entered " << number << '\n' ;
}