C++: F, C, and K temp. conversion

I am trying to make my first small program that will convert all of these temperatures, I think I am still a little bit off. Can I get some help?
--------------------------------------------------------------------------------

#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int ftemp;
int ctemp;
int ktemp;
int select = -1;


while (select ==-1)
{
cout << "Please select from the following (0 to quit): " <<endl;
cout << "1) Fahrenheit-to-Celsius" << endl;
cout << "2) Celsius-to-Fahrenheit" << endl;
cout << "3) Kelvin-to-Celsius" << endl;
cout << "4) Celcius-to-Kelvin" << endl;
cout << "5) Kelvin-to-Fahrenheit" << endl;
cout << "6) Fahrenheit-to-Kelvin" << endl;
cout << "Enter: ";
cin >> select;
if (select == 1)
{
cout << "Enter temperature in Fahrenheit to convert to degrees Celsius: ";
cin >> ftemp;
ctemp = (ftemp-32) * 5 / 9;
cout << "Equivalent in Celsius is: " << ctemp << endl;
}
if (select ==2)
{
cout <<" Enter temperature in Celsius to convert to degrees Fahrenheit: ";
cin >> ctemp;
ftemp = ctemp*9/5 + 32;
cout << "Equivalent in Fahrenheit is: " << ftemp << endl;
}
if (select ==3)
{
cout << "Enter temperature in Kelvin to convert to Celsius: ";
cin >> ktemp;
ctemp = (ktemp-273);
cout << "Equivalent in Celsius is: " << ctemp << endl;
}
if (select ==4)
{
cout << "Enter Celsius to convert to Kelvin";
cin >> ctemp;
ktemp=ctemp+273;
cout << "Equivalent in Kelvin is: " << ktemp << endl;
}
if
{
(select ==5)
cout << "Enter Kelvin to convert to Fahrenheit: ";
cin >> ftemp;
ftemp= (ktemp-459.67);
cout << "Equivalent in Fahrenheit:" << ftemp << endl;
}
if
{
(select ==6)
cout << "Enter Fahrenheit to convert to Kelvin: ";
cin >> ktemp;
ktemp= (ftemp+459.67);
cout << "Equivalent in Kelvin:" << ktemp << endl;
}
if (select ==0)
exit(0);
else
cout << "Valid options 1, 2, 3, 4, 5, or 6." << endl;
select = -1;
}

return 0;
}
Please use [code] tags.

Compiling:
a.cpp: In function 'int main()':
a.cpp:52:3: error: expected '(' before '{' token
a.cpp:60:3: error: expected '(' before '{' token

Once I fixed those, it ran OK, but did not loop like you want.

The main problem is with your exit conditions. Line 12 should read

while (select != 0)

This is because 0 is your exit condition. What you had makes everything except -1 your exit condition. The loop is supposed to exit when select is zero.


Next, get rid of line 71 -- you don't need to select = -1 any more, because if the user entered zero the loop will exit, otherwise it will continue.

This means you also do not need lines 67 and 68 any more either.


Finally, you want to complain if the user entered something invalid. You don't really have to, because if the user does, he/she gets the menu again. (To get that, just delete line 69.)

Otherwise, replace line 69 with an if for the condition(s) that is(are) invalid:

if (select < 0 || select > 6) // invalid if either of these is true

Hope this helps.
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
int ftemp;
int ctemp;
int ktemp;
int select = -1;


while (select !=-1)

cout << "Please select from the following (0 to quit): " <<endl;
cout << "1) Fahrenheit-to-Celsius" << endl;
cout << "2) Celsius-to-Fahrenheit" << endl;
cout << "3) Kelvin-to-Celsius" << endl;
cout << "4) Celcius-to-Kelvin" << endl;
cout << "5) Kelvin-to-Fahrenheit" << endl;
cout << "6) Fahrenheit-to-Kelvin" << endl;
cout << "Enter: ";
cin >> select;
if (select == 1)

cout << "Enter temperature in Fahrenheit to convert to degrees Celsius: ";
cin >> ftemp;
ctemp = (ftemp-32) * 5 / 9;
cout << "Equivalent in Celsius is: " << ctemp << endl ;]
if (select ==2)
[
cout <<" Enter temperature in Celsius to convert to degrees Fahrenheit: ";
cin >> ctemp;
ftemp = ctemp*9/5 + 32;
cout << "Equivalent in Fahrenheit is: " << ftemp << endl;
]
if (select ==3)
[
cout << "Enter temperature in Kelvin to convert to Celsius: ";
cin >> ktemp;
ctemp = (ktemp-273);
cout << "Equivalent in Celsius is: " << ctemp << endl;
]
if (select ==4)
[
cout << "Enter Celsius to convert to Kelvin";
cin >> ctemp;
ktemp=ctemp+273;
cout << "Equivalent in Kelvin is: " << ktemp << endl;
]
if
[
(select ==5)
cout << "Enter Kelvin to convert to Fahrenheit: ";
cin >> ftemp;
ftemp= (ktemp-459.67);
cout << "Equivalent in Fahrenheit:" << ftemp << endl;
]
if
[
(select ==6)
cout << "Enter Fahrenheit to convert to Kelvin: ";
cin >> ktemp;
ktemp= (ftemp+459.67);
cout << "Equivalent in Kelvin:" << ktemp << endl;
]


if (select < 0 || select > 6) // invalid if either of these is true
else
cout << "Valid options 1, 2, 3, 4, 5, or 6." << endl;



return 0;
}
How can I fix the "Error c2143: syntax error: missing ';' before ']' " on lines 30, 37, 44, 51, 59, and 67
Last edited on
Topic archived. No new replies allowed.