# Cant solve the syntax error.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-05-2009
brian75
Cant solve the syntax error.
I have a program that is supposed to convert temperature i celcius to temperature in fehrenhiet. I get a syntax error on the line where the user is supposed to input the temperature in celcuis.

I cant seem to figure out what is wrong.
I keep getting error: error C2059: syntax error : ';'

Any help would be appreciated. Thanks in advance.

Code:

```/* program to convert temperature in celcius   to temerature in fahrenheit */ #include <iostream> int c; //temperature in celcius int f = (9/5 * (c)) + 32; //temperature in fahrenheit int main() {         std::cout << "This program will convert your temperature in celcius to fahrenheit." "\n" "\n";         std::cout << "What is your current temperature in celcius? ";         std::cin >> c >>;                    //user inputs temerature in celcius         std::cout << "Your temperature in fahrenheit is " << f << "." "  Now go be tell your friends.";                                                //program converts output to fahrenheit         return (0); }```
• 11-05-2009
Ducky
Code:

```change std::cin >> c >>;  to std::cin >> c ;```
• 11-05-2009
brian75
thanks for the help. why do you have to remove the second set of >> ?
• 11-05-2009
Ducky
Why do you want two? :)

You are putting what you type into "c" with the oparator >>.

I you put a second one it means you wanna put it again somewhere else, which is not the case.
• 11-05-2009
brewbuck
This expression is wrong:

Code:

`int f = (9/5 * (c)) + 32; //temperature in fahrenheit`
As far as C++ is concerned, 9/5 is equal to 0. So this expression is the same as:

Code:

`int f = 32;`
If you really want to stick to integer math, you can rewrite the expression this way:

Code:

`int f = 9 * c / 5 + 32;`
This reorders the operations so that c is first multiplied by 9, then divided by 5. It's not precisely accurate due to integer round-off, but at least it doesn't output 32 all the time :)
• 11-06-2009
anon
Actually 9 / 5 equals 1.

The bigger problem is that the value of f is calculated before you give any input for c. You'll either need to move the calculation after the place where c gets a value, or turn it into a function instead.
• 11-06-2009
brian75
alright. would the equation:

Code:

`int f = ((9/5) * (c)) + 32`
work as well or does c++ read it still as 1? I'm at work now so I can't test the code out for myself now.

and when defining f i should do it after the user input instead when i declare the intergers?
• 11-06-2009
Dino
Using another language, here's an example of integer versus float math:

Code:

```> ((9/5) * (26)) + 32 58 > ((9.0/5.0) * (26)) + 32 78.8```
In C as well, just by adding the ".0" to the 9 and 5, the results are more correct. In your case, assigning to f, you would get 78.
• 11-06-2009
brian75
alright so to make my equation the most accurate i need to make it (9.0/5.0) when doing the conversion to fahrenhiet?
• 11-06-2009
darren78
Quote:

Originally Posted by brian75
alright so to make my equation the most accurate i need to make it (9.0/5.0) when doing the conversion to fahrenhiet?

You need to change the variable type from an int to one that accepts floating point numbers.
• 11-06-2009
Dino
Quote:

Originally Posted by brian75
alright so to make my equation the most accurate i need to make it (9.0/5.0) when doing the conversion to fahrenhiet?

No, to make it most accurate, you'll need to use floats or doubles, and, your constants should be the same data type too.
• 11-06-2009
brian75
so the beggining would want to look something like

Code:

```/* program to convert temperature in celcius   to temerature in fahrenheit */ #include <iostream> float c; //temperature in celcius float f = (9.0/5.0 * (c)) + 32.0; //temperature in fahrenheit```
for the most accurate read out.
• 11-06-2009
Elysia
double f = (9.0/5.0 * (c)) + 32.0; //temperature in fahrenheit
9.0 is a double constant, thus the result is a double.
Otherwise you need to add f to the end of the constants to make them into floats.
• 11-06-2009
brian75
Thank you for all your help everyone. I understand what was wrong now.
• 11-06-2009
Dino
OK, Brian - that's great.

Now, tell us. WHO was most helpful here? Huh, HUH????
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last