Centigrade->Farenehit program. Problem, please help.

This is a discussion on Centigrade->Farenehit program. Problem, please help. within the C++ Programming forums, part of the General Programming Boards category; Hey whats up all, I've been learning c++ for a few days and at college today we were given an ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    9

    Centigrade->Farenehit program. Problem, please help.

    Hey whats up all, I've been learning c++ for a few days and at college today we were given an assignment to create a program which takes an inputted value (centigrade) and outputs a farenheit value. The formula for which is: farenheit = 9/5 x centigrade + 32. My code is as follows:

    Code:
    #include<iostream>
    
    int main()
    
    {
    
      int farenheit;
      int centigrade;
      farenheit = (9/5)*(centigrade+32);
    
      std::cout << "Enter degrees centigrade :\n";
      std::cin >> centigrade;
      std::cout << "Farenheit is equal to: " << farenheit << std::endl;
      return 0;
    }
    well, it doesn't work, I was wondering if anyone could offer any insight or help? Forgive my ignorance as I've only been working with c++ for a few days now. Any help would be greated appreciated, peace.

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    use a double or a float, you don't want to use an integer for this.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    9
    Thanks for your reply man, it's solved part of the problem, although something still doesn't seem to work properly.

    eg: Centigrade 20 = 68 farenheit

    but no matter what value I enter for centirade, I get 32 for farenheit?

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    try writing the formula properly.


    Code:
    F  =   9C   +  32
           ---
            5

    => F=((9*C)/5.0)+32

    Also don't forget to insert the formula AFTER you have entered the variable 'centigrade'.

  5. #5
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    9/5 *C is the same thing as 9*C /5.
    But he is correct, you add the 32 after you do the multiplication.
    And you also need to do the math after the cent has been given the proper values from the user.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    You need to remember that C and C++ execute all commands in the order you put them in. The program you posted would actually do the following:

    farenheit = (9/5)*(centigrade+32);
    This line would first calculate 9/5 as integers (becuase you wrote them as integers) the end result will also be an integer, probably 2. (closest thing to 1.75) Next it will add 32 to the centigrade value (which hasn't been set yet so it could be everything), then it will multiply this by 2. Finally, it will assign the result of this calculation to the variable farenheit.
    After all that, it will ask the user to fill in a value for centigrade, which is then not used at all before the program ends, and doesn't influence the value of farenheit at all.
    Finally, what the other guys said. You'll want to use float variables for this and (not sure about this one, I don't care too much about imperial measurements) the +32 has to be done at the end of the formula.
    The correct program would (assuming I haven't made further mistakes) look like this:

    Code:
    #include<iostream>
    
    int main()
    {
      float farenheit;
      float centigrade;
    
      std::cout << "Enter degrees centigrade :\n";
      // FIRST get the centigrade value from the user
      std::cin >> centigrade; 
    
      // THEN calculate the farenheit value. 
      // Start the calculation off with centigrade so the return value 
      // will be a float, even though 9, 5 and 32 are integers
      farenheit = centigrade*9/5+32; 
      std::cout << "Farenheit is equal to: " << farenheit << std::endl;
      return 0;
    }
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  7. #7
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    Quote Originally Posted by Enahs
    9/5 *C is the same thing as 9*C /5.
    But he is correct, you add the 32 after you do the multiplication.
    And you also need to do the math after the cent has been given the proper values from the user.

    um, 9/5 *C is != 9C/5

    9/5 cannot be first the 9C has to be first calculation.

    since 9C/5+32 is 9 times the value of Centigrade, divided by 5 plus 32, not 9 divided by 5 times the value of centigrade plus 32. 9/5*C+32
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  8. #8
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    From here: http://www.condoconcepts.com/convers.htm

    Tf = (9/5)*Tc+32

    Tc = temperature in degrees Celsius
    Tf = temperature in degrees Fahrenheit

    Therefore, based upon the way this is defined, you need to follow that equation. Anything else will generate invalid results.


    Also, you must take in to account that in C++, doing integer division (9/5) will result in the value being chopped to an int (e.g. 9/5 = 1).

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout << 9/5 << endl;
    	cout << 9.0f/5.0f << endl;
    	cout << (float)9/(float)5 << endl;
    	return 0;
    }
    Will output:
    Quote Originally Posted by output
    1
    1.8
    1.8
    So keep that in mind when doing integer division. You must also keep in mind that the variable integer will only hold integer values. As stated above, to store floating point numbers (e.g. 2.422), you would use float, or double. Read up on C++ data types by doing a few Google searches.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Damn it jverkoey. You keep beating me to posts. Go over to young coders and give them some much needed direction. LOL.

    (Good to see ya my friend).

    Since (9/5) is a constant here - just precompute it.

    Code:
    float frac=9.0f/5.0f;
    Then use frac in place of 9/5. Makes it easier to read. You could also #define it.

  10. #10
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Quote Originally Posted by Jaqui
    um, 9/5 *C is != 9C/5

    9/5 cannot be first the 9C has to be first calculation.

    since 9C/5+32 is 9 times the value of Centigrade, divided by 5 plus 32, not 9 divided by 5 times the value of centigrade plus 32. 9/5*C+32
    Sorry man, you are wrong about the math.

    let's say X = 10

    9*X = 90
    90/5 = 18

    9/5 = 1.8
    1.8 * X = 18

    And to take 100 degrees C (because we should all know that is 212 F)
    [ (9/5) * 100 ] = 180
    180 + 32 = 212

    Or your method.
    9 * 100 = 900
    900 / 5 = 180
    180 + 32 - 212


    You can replace X with any number and it will still get the same exact thing for both methods. That is how multiplication and division work (they are really the same thing). Both ways work (assuming you do them correctly in c++).

    But his question about the c++ problem has been answered.

  11. #11
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Enahs
    Sorry man, you are wrong about the math.

    let's say X = 10

    9*X = 90
    90/5 = 18

    9/5 = 1.8
    1.8 * X = 18
    This is where you are incorrect about the math. 9/5 = 1, not 1.8. Get used to the fact that C++ does not use the same style of mathematical notation as what you have been used to.

    9 and 5 are integers, and C++'s division function over the integers is not the real-valued division function. That '/' in '9/5' is not the same operator as the '/' in '9.0 / 5.0'.

    Quote Originally Posted by Bubba
    Since (9/5) is a constant here - just precompute it.
    Code:
    float frac=9.0f/5.0f;
    Or... you could just write 1.8 ;-)

  12. #12
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    This is where you are incorrect about the math. 9/5 = 1, not 1.8. Get used to the fact that C++ does not use the same style of mathematical notation as what you have been used to.
    Which is why I said:
    "(assuming you do them correctly in c++)"

    We where not discussing c++, but the actual math. Implementing the math in c++ is different though, yes.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Jaqui
    um, 9/5 *C is != 9C/5

    9/5 cannot be first the 9C has to be first calculation.

    since 9C/5+32 is 9 times the value of Centigrade, divided by 5 plus 32, not 9 divided by 5 times the value of centigrade plus 32. 9/5*C+32
    Its been a couple years since I was in school, but 9/5*10 is the same as 10*9/5 which is the same as 9*10/5 because of the associative properties of mathametics . So 9/5*C = 9*C/5 = C*9/5.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 01:10 AM
  2. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Problem with Program not Quitting
    By Unregistered in forum Windows Programming
    Replies: 20
    Last Post: 06-11-2002, 11:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21