when I run this program no matter what temperature I enter, the output is my first cout statement.
Could anyone tell me what i must change for the code to work??

#include <iostream>
using namespace std;

int main ()

{

	
	double temp;
	cout << "Please enter a temperature " << endl;
	cin >> temp;
	
	if (temp = -173)
		cout << "ethyl alcohol freezes at that temperature" << endl;
	else if (temp = 172)
		cout << "ethyl alcohol boils at that temperature " << endl;
	else if (temp = -38)
		cout << "Mercury freezes at that temperature " << endl;
	else if (temp = 676)
		cout << "Mercury boils at that temperature " << endl;
	else if (temp = -362)
		cout << "Oxygen freezes at that temperature " << endl;
	else if (temp = 306)
		cout << "Oxygen boils at that temperature " << endl;
	else if (temp = 32)
		cout << "Water freezes at that temperature " << endl;
	else if (temp = 212)
		cout << "Water boils at that temperature " << endl;
	else
		cout << "No information found for the temperature you entered"<< endl;
		
	return 0;

}

if( variable is equal to a Number )
the do this code;

Translation :

if( temp == -173 ) 
   //do stuff

what you are doing is this :

if ( assign a number to temp )
//then do this

Translation :

if( temp = 100) //do stuff;

And generally, when you have more than 2 if-else, in which it could be replaced by a switch statement, then use
a switch statement.

const int BOILING_POINT  = 100;
const int FREEZING_POINT = -100;
const int EQUILIBRIUM_POINT = 0;

cout<<"Enter a temperature : ";
int tmpInDegrees = 0;
cin >> tmpInDegrees;

switch(tmpInDegrees)
{
  case BOILING_POINT :   cout<<"Boiling point \n"; break;
  case FREEZING_POINT : cout<<"Freezing point\n"; break;
  case EQUILIBRIUM_POINT  : cout <<" Equilibrium point\n"; break;
  default : cout<<"Nothing here for that temperature\n"; break;
}

Edited 7 Years Ago by firstPerson: n/a

The code you have written has a big error ...
While comparing values you should use "==" & not "=" as you have done

So the condition should be...


if(temp==-173)
.
.
.
else if (temp == 172)
..
.
and so on...


this should solve your problem

This article has been dead for over six months. Start a new discussion instead.