NO

Author Topic: Code repeating Problem  (Read 2033 times)

Louie

  • Guest
Code repeating Problem
« on: April 19, 2013, 08:46:23 PM »
Just starting to learn C and typed in a program from on page 12 of the second edition of Kernighan and Ritchie.
The program is supposed to print a Fahrenheit to Celsius conversion table such as:
0    -17.8
20    -6.7
40     4.4
and so on till it reaches 300 Fahrenheit
The code builds and compiles with no errors, but when it prints in DOS it just keeps repeating the first line 0   -17.8 and does not stop repeating.
The code follows:

Code: [Select]
#include <stdio.h>

/*  print Fahrenheit - Celsius table  */

int main(void)

{

float fahr, celsius;
int lower, upper, step;

lower = 0 ;            /* lower limit of temperatures table */
upper = 300 ;      /* upper limit */
step = 20 ;           /* step size */

fahr = lower;
while (fahr <= upper)  {
celsius = (5.0/9.0) * (fahr-32.0);
printf("%3.0f %6.1f\n", fahr, celsius);
fahr = fahr * step;
}
return 0;

}
Any help on this would be appreciated. I also typed in the program on page 15 (same book) using the #define symbolic constants and get the same exact results.
« Last Edit: April 19, 2013, 09:30:35 PM by Stefan Pendl »

czerny

  • Guest
Re: Code repeating Problem
« Reply #1 on: April 19, 2013, 10:16:09 PM »
Code: [Select]
fahr = fahr + step;

Louie

  • Guest
Re: Code repeating Problem
« Reply #2 on: April 19, 2013, 11:39:15 PM »
Thanks Czerny
I need to pay closer attention to the expressions in the book
This is an interesting language and hope to learn more in the future.
Thanks again