Thread: Cant solve the syntax error.

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    35

    Question 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);
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Code:
    change
    
    std::cin >> c >>;   
    
    to
    
    std::cin >> c ;
    Using Windows 10 with Code Blocks and MingW.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    thanks for the help. why do you have to remove the second set of >> ?

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    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.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    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?

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    alright so to make my equation the most accurate i need to make it (9.0/5.0) when doing the conversion to fahrenhiet?

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by brian75 View Post
    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. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by brian75 View Post
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    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.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    Thank you for all your help everyone. I understand what was wrong now.

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    OK, Brian - that's great.

    Now, tell us. WHO was most helpful here? Huh, HUH????
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PlaySound
    By cangel in forum C++ Programming
    Replies: 16
    Last Post: 10-08-2009, 05:29 PM
  2. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  3. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM