If statements and such :(

so basically im trying to make a program which converts temperatures, and I was wondering if there is any better way to represent the code im going to post, i think the syntax is right, Ive tried a switch statement and still this isnt working properly at all :(


if(userInput=3)
celsius = kelvin - 273.15;
fahrenheit = (kelvin*1.8)-459.67;
kelvin = temperature;

if (userInput=2)
celsius = (fahrenheit-32)*(5/9);
fahrenheit = temperature;
kelvin = (fahrenheit + 459.67)*(5/9);

if(userInput=1)
celsius = temperature;
fahrenheit = (celsius*1.8)+32;
kelvin = celsius+273.15;


thanks for your help
= is the assignment operator.
== is the equality operator.
anything wrong with this?
cause this doesn't work either


if(userInput==1){
celsius=temperature;
fahrenheit = (celsius*1.8)+32;
kelvin = celsius+273.15;
}
else if(userInput==2){
fahrenheit = temperature;
celsius = (fahrenheit-32)*(.556);
kelvin = (fahrenheit + 459.67)*(.556);
}
else{
kelvin = temperature;
celsius = kelvin - 273.15;
fahrenheit = (kelvin*1.8)-459.67;
}

Only thing I think could be causing problems are the (.556)...try (0.556) instead.
I made those changes but it seems that everytime i run it its just going to the else statement every time now


:(
Let's see the whole program.
void runProblem2Solution()
{
float celsius=0, fahrenheit=0, kelvin=0, temperature;
enum TemperatureUnit
{
TU_CELSIUS = 1,
TU_FAHRENHEIT,
TU_KELVIN
};
TemperatureUnit temperatureUnit;
char userInput;

cout << "Enter the value of the temperature: ";
cin >> temperature;

do
{
cout << "Which unit is this temperature?" << endl
<< "\t[1] Celsius" << endl
<< "\t[2] Fahrenheit" << endl
<< "\t[3] Kelvin" << endl
<< "> ";
cin >> userInput;
}
while( userInput < '1' || userInput > '3' );

temperatureUnit = static_cast<TemperatureUnit>(userInput-'0');
userInput = 0;


if(userInput==1){
celsius=temperature;
fahrenheit = (celsius*1.8)+32;
kelvin = celsius+273.15;
}
else if(userInput==2){
fahrenheit = temperature;
celsius = (fahrenheit-32)*(0.556);
kelvin = (fahrenheit + 459.67)*(0.556);
}
else{
kelvin = temperature;
celsius = kelvin - 273.15;
fahrenheit = (kelvin*1.8)-459.67;
}




cout << " Temperature in Celsius: " << celsius << endl
<< "Temperature in Fahrenheit: " << fahrenheit << endl
<< " Temperature in Kelvin: " << kelvin << endl;

if( kelvin < 0 )
cout << "These temperatures cannot exist in Nature, as they are less than absolute zero." << endl;
}
temperatureUnit = static_cast<TemperatureUnit>(userInput-'0');: temperatureUnit is never used.
userInput = 0; before the if effectively nullifies the user's input. The if should be: ...=='1', ...=='2'.
Last edited on
enum={TempUnit};/*need the assignment operator + curly brackets with enum*/
i don't know much about the math but think it should work if you delete 'else' *2 and make 3 straight if statements
Topic archived. No new replies allowed.