Hey guys,

Here's what I have to do:

Write a program that displays the table of Fahrenheit - Celsius temperatures.
The formula is : C = (5/9) (F-32)
C - Celsius, F- Fahrenheit
The range should be: 0 - 200, where as the step should be set to 20.
The output should be:
Fahrenheit Celsius
0 -17.8
20 -6.7
40 4.4
……
200…..
Define the floating point variables for the Celsius to be of type float and use the %f
format code.
Note that the sequence %.1f in printf indicates that the floating point should be
displayed with 1 character after decimal point. And %.4f - indicates a display of
floating-point with 4 characters after the decimal point.

Here's the code I have so far:

#include <stdio.h>     
#include <simpio.h>    
#include <genlib.h>                                                                       

main()
{
float c, f;

for (f=0,f<200,f++20);
  {
   C = (5/9)*(F-32);
    printf("%f     %f",f,c);

}



getchar();

}

I'm not sure what I'm doing wrong, could someone give me some hints as to point me in the right direction? I'm still very noobish at C, so I apologise if their are obvious errors, because I don't see them...

(5/9) is doing integer division, not floating point division, which means that all fractions are lost, so the result of that is 0. To correct that problem add a decimal point, e.g. (5.0/9.0) or (5.0/9) or (5/9.0). As long as numerator or denominator or both are floating point numbers it will do floating point math.

Edited 4 Years Ago by Ancient Dragon

**I corrected that, but it still doesn't seem to be working. *

Here are the errors I'm getting:

Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp" -o "C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..exe"    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib" 
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp: In function `int main()':
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:8: error: expected `;' before numeric constant
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:9: error: expected primary-expression before '{' token
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:9: error: expected `;' before '{' token
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:9: error: expected primary-expression before '{' token
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:9: error: expected `)' before '{' token
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:10: error: `C' undeclared (first use this function)
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:10: error: (Each undeclared identifier is reported only once for each function it appears in.)
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:10: error: `F' undeclared (first use this function)

Execution terminated

Refer to the comments I have added to your original code.

#include <stdio.h>
#include <simpio.h>  /* Remove this */
#include <genlib.h>  /* Remove this */

main()  /* Should be int main(void) */
{
    float c, f;  /* Should be int f; float c; */

    for (f=0,f<200,f++20);  /* Should be for (f = 0; f < 200; f += 20) */
    {
        C = (5/9)*(F-32);  /* Should be c = (5.0 / 9.0) * ((float)f - 32.0); */
        printf("%f     %f",f,c); /* Should be printf("%d  %.1f", f, c); */
    }

    getchar();
}

Thanks for the help, but I'm still getting 5 errors...

Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp" -o "C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..exe"    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib" 
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp: In function `int main()':
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:7: error: expected `;' before ')' token

C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:13: error: expected primary-expression before '}' token
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:13: error: expected `)' before '}' token
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:13: error: expected primary-expression before '}' token
C:\Users\HyperCriticality\Desktop\celsiusfahrenheit..cpp:13: error: expected `;' before '}' token

Execution terminated

@Hyperion101:

Are you placing the semicolons correctly? Please take special notice of the for loop.

Thanks avishek! I had the syntax of the for loop wrong, it's all working now! <3

This question has already been answered. Start a new discussion instead.