I am writing a program that will allow the user to in put a temp and program will tell which of these four elements will freeze or boil.
Substance Freezing point Boiling Point
Ethyl Alcohol -173 172
Mecury -38 676
Oxygen -362 -306
Water 32 212
I have written the code but for some reason I cannot get it to display correctly.
Here is my code.
[code]
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Variables
short temp;
char answer;
do
{ cout << "Please enter a temperature: " ;
cin >> temp;
if (temp > 677 )
cout << "Ethyl Alcohol, water, Oxygen and Mecury will boil." << endl;
else if (temp > 212 && temp < 676)
cout << "Ethyl Alcohol, Oxygen and water will boil." << endl;
else if (temp > 172 && temp < 212)
cout << "Ethyl Alcohol will boil." << endl;
else if (temp < 32 && temp > -38)
cout << "Water freezes and Oxygen boils." << endl;
else if (temp < -38 && temp > -173)
cout << "Water and Mercury freezes and Oxygen boils." << endl;
else if (temp < -173 && temp > -362 )
cout << "water, Mercury, and Ethyl Alcohol freeze and Oxygen boils." << endl;
else if (temp <= -362)
cout << "Water, Mercury, Ethyl Alcohol, and Oxygen freeze.\n";
//asks if user want to try again.
cout << "Would you like to try again? (y or n)";
cin >> answer;
}while (answer == 'Y' || answer == 'y');
What do you mean "it doesn't display correctly". Please be specific.
I ran your program, entered 1000 and got "Ethyl Alcohol, water, Oxygen and Mecury will boil." as expected.
You do however have a problem with your conditions.
For example, if you enter 212, nothing displays.
This is because your second if checks temp > 212, while the third if checks temp < 212. What happens if temp is exactly 212? Nothing.
You also have a problem with a missing if statement for temps between 32 and 172.
PLEASE USE CODE TAGS (the <> formatting button) when posting code. http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.