Hello everyone,
I'm very new to programming and taking an intro course for fun. I have a project due tonight that I wanted to spice up a bit if I can. Here is my code:
//This program is designed to output which substances will either freeze or boil
//given a user generated temperature.
#include <iostream>
using namespace std;
int main()
{
//Temperature variable input from user.
int temp;
//Display of programs purpose.
cout << "Welcome to the Freeze & Boil Program!\n";
cout << "This program will take any temperature you type in\n";
cout << "and display what substances on record will freeze or boil\n";
cout << "at that point.\n" << endl;
//Program prompts user to input a temperature to be tested.
cout << "Please enter a temperature in degrees Farenheit:\n";
cin >> temp;
//Program processes given temperature so it may output each corresponding substance.
if
cout << "That is not a valid temperature. Please try again.\n";
cout << endl << "Subsances that will freeze:\n";
if (temp <= -173)
cout << "Ethyl Alcohol\n";
if (temp <= -38)
cout << "Mercury\n";
if (temp <= -362)
cout << "Oxygen\n";
if (temp <= 32)
cout << "Water\n";
cout << endl << "Substances that will boil:\n";
if (temp >= 172)
cout << "Ethyl Alcohol\n";
if (temp >= 676)
cout << "Mercury\n";
if (temp >= -306)\
cout << "Oxygen\n";
if (temp >= 212)
cout << "Water\n";
cout << endl << "Thank you for using the Freeze & Boil Program!" << endl;
cin.get();
return 0;
}
I know it's a very simple and common program. So far The program works correctly (at least with the tests I've attempted) but I wanted to add to it the ability to catch any input that isn't a number. Such as a-z or A-Z. You can see I've started the If function with a complementing cout statement.
Keeping in mind I'm still a novice is there an easy way for me to code in what I need. I know it's possible but I'm trying to stay close to the beginner level.
You'll want to check to see if cin is in an error state:
1 2 3 4 5 6 7 8
if(std::cin>>temp) {
//we got a value
} else {
//there was a problem...do something
//don't forget to clear the stream of the bad data!
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.clear();
}
I replaced line 24 with cin.fail(), so if user enters a non-integer then it will display "The temp is invalid".
Then I put everything else in the else { } brackets, so they get compiled only if the temperature is valid.
//This program is designed to output which substances will either freeze or boil
//given a user generated temperature.
#include <iostream>
usingnamespace std;
int main()
{
//Temperature variable input from user.
int temp;
//Display of programs purpose.
cout << "Welcome to the Freeze & Boil Program!\n";
cout << "This program will take any temperature you type in\n";
cout << "and display what substances on record will freeze or boil\n";
cout << "at that point.\n" << endl;
//Program prompts user to input a temperature to be tested.
cout << "Please enter a temperature in degrees Farenheit:\n";
cin >> temp;
//Program processes given temperature so it may output each corresponding substance.
if (cin.fail())
cout << "That is not a valid temperature. Please try again.\n";
else{
cout << endl << "Subsances that will freeze:\n";
if (temp <= -173)
cout << "Ethyl Alcohol\n";
if (temp <= -38)
cout << "Mercury\n";
if (temp <= -362)
cout << "Oxygen\n";
if (temp <= 32)
cout << "Water\n";
cout << endl << "Substances that will boil:\n";
if (temp >= 172)
cout << "Ethyl Alcohol\n";
if (temp >= 676)
cout << "Mercury\n";
if (temp >= -306)\
cout << "Oxygen\n";
if (temp >= 212)
cout << "Water\n";
cout << endl << "Thank you for using the Freeze & Boil Program!" << endl;
cin.get();
}
return 0;
}