I am completely new to C++ and am very confused... I just wrote this, and when I enter letters instead of numbers the program crashes. What can I do to fix this so that if letters are entered, it will display the invalid message? Thanks!
#include <iostream>
usingnamespace std;
int main ( )
{
// Solicit input from the user
cout << "Enter temperature in Fahrenheit: ";
int temp; // Variable holding user input temperature
cin >> temp;
// Display the results according to user input
if (temp <= -362)
{
cout << "\nOxygen, Ethyl alcohol, Mercury and water will ";
cout << "freeze at this temperature." << endl;
}
elseif (temp >= -306 && temp <= -173)
{
cout << "\nEthyl alcohol, Mercury and water will freeze ";
cout << "and Oxygen will boil at this temperature." << endl;
}
elseif (temp > -173 && temp <= -38)
{
cout << "\nMercury and water will freeze ";
cout << "and Oxygen will boil at this temperature." << endl;
}
elseif (temp > -38 && temp <= 32)
{
cout << "\nWater will freeze and Oxygen will boil ";
cout << "at this temperature." << endl;
}
elseif (temp > 32 && temp < 172)
{
cout << "\nOxygen will boil at this temperature." << endl;
}
elseif (temp >= 172 && temp < 212)
{
cout << "\nOxygen and Ethyl alcohol will boil ";
cout << "at this temperature." <<endl;
}
elseif (temp >= 212 && temp < 676)
{
cout << "\nOxygen, Ethyl alcohol and water will boil ";
cout << "at this temperature." << endl;
}
elseif (temp >= 676)
{
cout << "\nOxygen, Ethyl alcohol, water and Mercury ";
cout << "will boil at this temperature." << endl;
}
else
cout << "\nYou have entered an invalid value." << endl;
// Hold the result on screen for the user to read.
cin.ignore( );
cout << "\nPress Enter to exit the program.";
cin.get( );
//return 0;
}
You could use <string>instead <int>.
But you should make the if stetmanets using this "".
1 2 3 4 5 6 7
for example:
string temp;
if (temp <= "-362")//here you mean that it is string.
{
cout << "\nOxygen, Ethyl alcohol, Mercury and water will ";
cout << "freeze at this temperature." << endl;
}
So now your program whatever you put will take it as string,so will not crash
Thanks. I tried using string instead. The program does not crash any more but it treats the letters and symbols entered as numbers, and are showing the results as if I entered a numeric value, not the "invalid value" message. It's so puzzling!
A good way to do it is to read input into a string, then convert that string to the intended type with a stringstream, repeating until the user enters valid input. The example below uses a template parameter to allow it to work with all basic types (and then some).
#include <iostream>
#include <sstream>
usingnamespace std;
template<class value_type> //template allows it to work with all basic types
value_type get_value(string message)
{
value_type output;
string input;
while(true) {
cout << message;
getline(cin, input); //read input as a string until user presses ENTER/RETURN
if(cin) { //if you read a string successfully...
stringstream extract(input); //convert input to a new stream...
if(extract >> output //if the stream successfully converts to the intended type
&& !(extract >> input)) //and no input is left in the stream
return output;
}
else cin.clear();
cerr << "\nInvalid input. Please try again.\n"; //you didn't enter a valid value if you get here
}
}
int main()
{
int i = get_value<int>("Please enter an integer: "); //Examples: notice the type name in <>'s for each function call
double d = get_value<double>("Please enter a double: ");
string s = get_value<string>("Please enter a string: ");
}
If you need to check for ranges, this function can easily be modified to do so...