Thread: Temperature Help

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    18

    Temperature Help

    I am working on a temp chart conversion. We have the the conversion from Celsius to Fahreneheit rounded to whole numbers but the conversion from Fahrenheit to Celsius keep putting decimals out. I need it to output whole numbers.
    Here is the code:

    [CODE]
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;

    int main()
    {
    ofstream outFile ("ass3.run");
    const int LOWER = 0;
    const int UPPER = 400;
    const int STEP = 10;

    int FARR = LOWER;
    float Celsius;

    /* Print table heading */
    outFile <<setw(19);
    outFile << "Fahrenheit";
    outFile <<setw(12);
    outFile << "Celsius";
    outFile <<setw(20);
    outFile << endl;

    /* print table from LOWER to UPPER */
    for (FARR = LOWER ; FARR <= UPPER ; FARR += STEP)
    {
    int Fahrenheit (int);
    outFile <<setw(15);
    outFile << FARR;
    Celsius = (float(5)/float(9)) * (FARR - 32);
    outFile <<setw(15);
    outFile << Celsius;
    outFile <<setw(15);
    outFile <<endl;
    }
    const int MONEY=0;
    const int STUPID =200;
    const int GIRL= 5;

    int CELS = MONEY;
    float FAR;
    outFile <<setw(19);
    outFile <<endl<<endl;
    outFile << "Celsius";
    outFile <<setw(15);
    outFile << "Fahrenheit";
    outFile <<setw(20);
    outFile << endl;

    for (CELS = MONEY; CELS <= STUPID ; CELS += GIRL)
    {
    int Celsius (int);
    outFile <<setw(15);
    outFile << CELS;
    FAR = (float(9)/float(5) * (CELS) + 32);
    outFile <<setw(15);
    outFile << FAR;
    outFile <<setw(15);
    outFile <<endl;
    }

    return 0;
    }
    [CODE]
    This is the output we keep getting:
    Fahrenheit Celsius
    0 -17.7778
    10 -12.2222
    20 -6.66667
    30 -1.11111
    40 4.44444
    50 10
    60 15.5556
    70 21.1111
    80 26.6667
    90 32.2222
    100 37.7778
    110 43.3333
    120 48.8889
    130 54.4444
    140 60
    150 65.5556
    160 71.1111
    170 76.6667
    180 82.2222
    190 87.7778
    200 93.3333
    210 98.8889
    220 104.444
    230 110
    240 115.556
    250 121.111
    260 126.667
    270 132.222
    280 137.778
    290 143.333
    300 148.889
    310 154.444
    320 160
    330 165.556
    340 171.111
    350 176.667
    360 182.222
    370 187.778
    380 193.333
    390 198.889
    400 204.444


    Celsius Fahrenheit
    0 32
    5 41
    10 50
    15 59
    20 68
    25 77
    30 86
    35 95
    40 104
    45 113
    50 122

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Fun with iomanip (it makes me yearn for printf):
    Code:
    #include <iostream>
    #include <iomanip>
    
    namespace {
      const int UPPER = 400;
      const int LOWER = 0;
      const int STEP = 10;
    }
    
    int main()
    {
      std::cout.precision ( 0 );
      for ( int f = LOWER; f <= UPPER; f += STEP )
        std::cout<< f <<' '<< std::fixed
                 << ( ( 5.0 / 9.0 ) * ( f - 32 ) )
                 <<std::endl;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    18

    Thank you

    We struggled with the decimals for over three hrs, I appreciate your help Prelude. Hopefully this works.

    Shawn

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Nice try on the code tags. You can edit your post (usually), so edit it and add '/' without the quotes to your second code tag like this [/CODE]. That will make it much more readable (assuming you have readable indentation).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Temperature Conversion code problems
    By eroth88 in forum C Programming
    Replies: 6
    Last Post: 10-22-2006, 01:24 AM
  2. Motherboard Temperature
    By MK4554 in forum Windows Programming
    Replies: 1
    Last Post: 07-18-2006, 10:51 AM
  3. Temperature conversion...
    By Onslaught in forum C Programming
    Replies: 3
    Last Post: 10-21-2005, 01:15 PM
  4. read the CPU temperature
    By BianConiglio in forum Windows Programming
    Replies: 2
    Last Post: 05-19-2004, 11:41 AM
  5. functions ??? help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2001, 02:33 AM