0
votes

I am learning C and need some help. The task is to use a while loop to output a table of Fahrenheit temperatures and their Celsius equivalents. I am going from 1 degree Fahrenheit (-17 Celsius) to 300 degrees Fahrenheit (148 Celsius) with increments of 20 degrees Fahrenheit. I want to output both temperatures with 2 decimal places (I am using %.2f). I am getting the following error:

22:33: warning: expression result unused
  [-Wunused-value]
while ((i = upperbound), (i >= lowerbound), (i=i - increment))
                          ~ ^  ~~~~~~~~~~
1 warning generated.

So lowerbound for some reason isn't being used. I have tried several things and none have worked. Please help! Here is the code:

#include <stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>

int main()
{
    double Tc=0;
    double Tf=0;
    double lowerbound = 1;
    double upperbound = 15;
    double increment = 1;
    int i = 15;
    while ((i = upperbound), (i >= lowerbound), (i=i - increment))
    {
        Tf=20*i;
        Tc=((5.0*Tf)-(5.0*32.0))/9.0;
        printf("%.2f\t%.2f\n", Tf, Tc);
    }
    printf("1.000000    %f\n",((5.0*1)-(5.0*32.0))/9.0);
    return 0;
}
2
It looks like you are mixing up a for loop with while loop and using , instead of ;. - Shafik Yaghmour
No, the teacher said I had to use a while loop. I have a for loop working with the same program! - user15713

2 Answers

3
votes
while ((i = upperbound), (i >= lowerbound), (i=i - increment))

Why are there , in while? I think you meant for and , should be ;:

for ((i = upperbound); (i >= lowerbound); (i=i - increment))

If you are required to use a while loop, you cannot create your own syntax by mixing both! The syntax of while is the following:

while(expression)
{
     instruction1;
     instruction2;
     ...
}

where expression is one expression, which either is true or false. In your case this would be i >= lowerbound. You cannot put the incrementation and initialization of the loop variable in there, you have to do this separately. The initialization should be before the loop and the increment should be done at the end of the instruction block of the loop. Then the while loop is equivalent to the for loop.

Also it seems you are better of with int for upperbound, lowerbound and increment, as floating points are not required to be exact.

1
votes

As you are inerested in using while loop, try this

 #include <stdio.h>
 #include<math.h>
 #include<string.h>
 #include<stdlib.h>

 int main()
 {
     double Tc=0;
     double Tf=0;
     int lowerbound = 1;
     int upperbound = 15;
     int i = upperbound;
     while (i >= lowerbound)
     {
          Tf=20*i;
          Tc=((5.0*Tf)-(5.0*32.0))/9.0;
          printf("%.2f\t%.2f\n", Tf, Tc);
          i--;
     }
     printf("1.000000    %f\n",((5.0*1)-(5.0*32.0))/9.0);
     return 0;
 }  

See the output here: http://ideone.com/DuYi0j