Temperature conversion program

Good day to all,

I need to get this temperature conversion program working, but when I run it I get this:

0 0

0 0

0 0

0 0

.....


Here is the code:

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

#include<stdio.h>


int x(int step)
{
   int celsius, fahr;
   celsius = 5 * (fahr-32) / 9;
   fahr = fahr + step;
   return celsius;
}


int main()
{

    int fahr, celsius;
    int lower, upper;
    
    lower = 0;
    upper = 300;
    
    
    fahr = lower;
    while (fahr <= upper) {
    x(20);
    printf(" %d\t%d\n " ,  fahr, celsius);
    
    
  }
}




Thank you in advance :)
I think you are wrong with the function. Take a look at this

1
2
3
4
5
6
7
int x(int step)
{
   int celsius, fahr;
   celsius = 5 * (fahr-32) / 9;
   fahr = fahr + step;
   return celsius;
}


where is the value of variable"fahr"?
The function only accepts the value of variable "step" passed to it. Therefore the function has undefined values and didn't work.
As a note you should use double instead of int for the temperature variables in case you wont get any wrong answers.
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<stdio.h>


float x(float fahr)
{
   float celsius;
   celsius = 5.0 * (fahr-32.0) / 9;
   return celsius;
}


int main()
{

    float fahr, celsius;
    
    
    int lower = 0;
    int upper = 300;
    int step = 20;
    
    
    fahr = lower;
    while (fahr <= upper) {
    celsius = x(fahr);
    printf(" %f\t%f\n " ,  fahr, celsius);
    fahr = fahr + step;
    
    
  }
}
      

 


How would this look like :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;

double x(double fahr)
{
   double celsius;
   celsius = 5.0 * (fahr-32.0) / 9;
   return celsius;
}

int main()
{
    for(int i=0; i<=300; i++)
    {
    	if(i%20==0)
    	{
    		cout<<x(i)<<"\t";
    	}
    }
    
 return 0;   
}



Dont use while loop here. A for loop makes clear. And you are wrongly defining unnecessary types like float fahr, it would take infinite loop like starting from 0>0.00001>0.00002(like this)so use only int for both while and for loops. Hope you are ok :)
Last edited on
Ok but 'namespace' is a C++ concept, and I don't know C++. There is no such thing as 'namespace' in plain C.
Sorry, C dont use Namespace. You can write without it. Cout is the same as printf.
This is a C++ site, so examples in C++ are not totally unexpected.

I would refine the Kyi's version a bit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
// no using namespace; there is exactly one identifier that we will use, once

double x( int tempF ) // our loop will supply integers
{
   return (tempF-32) * 5.0 / 9.0;
}

int main()
{
  // named constants make code more clear
  constexpr int Upper {300};
  constexpr int Step  {20}
  int fahr {0};
  for ( ; fahr <= Upper; fahr += Step )
  {
      std::cout << fahr << '\t' << x( fahr ) << '\n';
  }  
 return 0;   
}

There is nothing wrong in using while, but the loop counter is indeed best kept as integral value. You can always convert/cast integer to float, if needed.

When you have fahr<=upper, where fahr is float and upper is int, the operator<= must cast the upper to float. Likewise for the fahr+step. The Fahrenheit temparatures are clearly integer values, so why not keep them as such?
Topic archived. No new replies allowed.