Celsius Temperature Table not working?

The question that keeps arising time and time again is the following, "Why is the code not properly displaying the accurate results?"

I have searched similar coding and they are no less or different than the one I present before you. I've tried different methods and data types; however, I get numbers either being a constant zero or 1 degrees in Fahrenheit being -17.92, when the following result should be played near the zero value. Yet, the following value for zero is 2.58e+267, resulting a value close to 268. In addition, I had a following error that stated that zero cannot be read in the function. It may be the Dev-Cpp compiler, the sensitivity during the programming, or the user unable to comprehend a simple task. Nonetheless, if anyone is capable of aiding my issue, I appreciate it.

The following code is the recent design and the previous have been overwritten with constant: trail and error, research, reading and analysis, as well as a hard copy (i.e written by hand).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

double celsius (int f);

int main ()
{

double  temperatureC;

cout << "Fahrenheit\t\tCelsius\n\n";

//F for Fahrenheit and C for Celsius
for (int f = 0; f <= 20; f++)
    {
       
       cout << fixed << showpoint << setprecision(2);
       
       cout << setw(5) << f++ << "\t\t " << setw(13) << temperatureC << endl;
       temperatureC = celsius(f);
       
       cout << "\n\n"; 
    }

system ("pause"); 
return 0;
}
double celsius(int f)
{      
       
       double temperatureC;
       
       return temperatureC = ((f-32)*5)/9;
}
Last edited on
Since f is an integer type, ((f-32)*5)/9 does integer division, so the result will always be an integer.
The easiest way to fix that is to change one of the constants to a floating-point value, like ((f-32)*5)/9.0 for instance.

Also, your for loop is really confusing. It seems like it increments f, then prints the previous value of temperatureC (which, by the way, will be a garbage value during the first loop iteration since it hasn't been set yet, which is why you're getting a garbage value for f=0).
Then because of the for loop, f gets incremented again, which is why it skips every other value.

Try moving line 21 above line 20 and change that f++ on line 20 to just f.
Thanks. Also, I apologize for not adding #include <iomanip>, forgot placing the thing up there when I was typing it down. Anyway, I've modified the code and ran a few tests. The digit required being changed in decimal form. In addition to, a little bit of clean up like you suggested:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>
using namespace std;

double celsius(int f);

int main()
{

    cout << "Fahrenheit" << setw(20) << "Celsius" << "\n\n";
    
    cout << fixed << showpoint << setprecision(2);
    
    for (int fahrenheit = 0; fahrenheit <= 20; fahrenheit++)
    {
        cout << setw(5) << fahrenheit << setw(25) << celsius(fahrenheit) << "\n\n";
    }
    
    system("pause");
    return 0;
}

double celsius(int f)
{
    return (double) ((f-32)*5.0)/9.0;
}


It runs perfectly now, though I wished I had solved the issue sooner to get a better grade. Nonetheless, thank you.
Last edited on
Topic archived. No new replies allowed.