Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: C++ Code Help

  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Location
    My room
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C++ Code Help

    What's wrong with this code and how can I fix it?

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[]){
        //-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
        //Farenheit = Celsius * (212 - 32)/100 + 32
        int nCelsius;
        cout << "Enter the temperature in Celsius:"; << endl;
        cin >> nCelsius >> endl;
        //-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
        //Calculate convershion factor for Celsius to Farneheit
        int nFactor;
        nFactor = 212 - 32
        //-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
        //Calculate convershion factor for Farenheit to Celsius
        int nFarenheit;
        nFarenehit = nFactor * nCelsius/100 + 32;
        //-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
        //Output the results
        cout << "Farenheit value is:" << endl;
        cin >> nFarenheit >> endl;
        //-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
        //Keep the window open
        system("PAUSE");
        return 0;
        }
    The compiler says that there's a problem with line 10:

    10 C:\Program Files\MyProjects\Test Project\main\temperature.cpp expected primary-expression before '<<' token

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try to correct the marked parts:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[]){
        //-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
        //Farenheit = Celsius * (212 - 32)/100 + 32
        int nCelsius;
        cout << "Enter the temperature in Celsius:"; << endl; // remove that ';'
        cin >> nCelsius >> endl;
        //-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
        //Calculate convershion factor for Celsius to Farneheit
        int nFactor;
        nFactor = 212 - 32 // <<< here is missing a ;
        //-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
        //Calculate convershion factor for Farenheit to Celsius
        int nFarenheit; // 34.5 is not of type int
        nFarenehit = nFactor * nCelsius/100 + 32;
        //-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
        //Output the results
        cout << "Farenheit value is:" << endl;
        cin >> nFarenheit >> endl;
        //-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
        //Keep the window open
        system("PAUSE");
        return 0;
        }
    regards
    Last edited by oesxyl; 10-18-2008 at 05:41 AM.

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Location
    My room
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh I see. The book that I'm using says that it's for Visual C++, so I assumed that whatever it said would work for Dev C++. I typed it exactly how it was written in the book, and overlooked that integer error.

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You need a better book. This example won't work in any C++ compiler.
    Code:
        cin >> nCelsius >> endl;
    cin >> endl will never compile. Same goes for:
    Code:
        //Output the results
        cout << "Farenheit value is:" << endl;
        cin >> nFarenheit >> endl;
    which will definitely not output any result.
    Code:
        int nFarenheit; // 34.5 is not of type int
        nFarenehit = nFactor * nCelsius/100 + 32;
    Changing the type of nFarenheit to float/double is not enough if you want a floating point result.
    In the division
    Code:
    nCelsius/100
    you divide int by int which will return you an int, not a floating point number. It should be
    Code:
    nCelsius/double(100)
    // or
    nCelsius/float(100)
    (you could also change the type of nCelsius to float or double)

    I don't see how any book, which has such errors, can teach anything properly.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    Well, AFAIK the conversion goes like this:
    f = c / (5.0/9.0) + 32;

    If you invert that 5/9 and then multiply rather than divide, you get
    f = c * 1.8 + 32


    This means the code (including both conversions from C to F and from F to C) should look something like this:
    Code:
    #include <iostream>
    using namespace std;
    
    double ftoc(double f) { return (f - 32) / 1.8; }
    double ctof(double c) { return c * 1.8 + 32; }
    
    int main()
    {
      double c, f;
    
      // Prompt for a temperature
      cout << "Enter the temperature in Celsius: ";
      cin >> c;
    
      // Covert the temperature
      f = ctof(c);
    
      // Display the result
      cout << c << " C converts to " << f << " F" << endl;
    
      return 0;
    }
    And when I say it should look something like that, I also mean that you should get rid of all the crap that you put in there just to make it unreadable.

    A sample output:
    Code:
    $ ctof
    Enter the temperature in Celsius: 26
    26 C converts to 78.8 F
    $
    (you can confirm this with google calculator: http://www.google.com/search?q=26+celsius+in+fahrenheit )
    Last edited by ghell; 10-19-2008 at 04:41 PM. Reason: 5/9 inverted to 1.8


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •