Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem in file input and output in C programming

$
0
0
Hello everyone. Firstly, I am newbie around here as well as in C programming and I am hoping you all can guide me. I have a problem in a code which requires me to convert temperature in fahrenheit into degree celcius which also requires me to use file input and output. Here is my code:


#include <stdio.h>

#define size 15

int main(void)
{
    int high, medium, low, i;
    float fahren[15], celcius[15], sum, averg;
    char grad[16];
    
    FILE *inp, *outp;
    
    inp = fopen("input.txt", "r");
    outp = fopen("output.txt", "w");
    
    for (i=0;i>size;i++) {fscanf(inp,"%f", &fahren[i]);}
    
    for (i=0;i>size;i++) {
        celcius[i] = 5/9 * (fahren[i] - 32);
        sum += celcius[i];}
    
    averg = sum/size;
    
    for (i=0;i>size;i++){
         if (celcius[i] >= 35){grad[i] = 'H'; ++high;}
         else if (celcius[i] < 35 && celcius[i] >= 20){grad[i] = 'M'; ++medium;}
         else {grad[i] = 'L'; ++low;}}
         
    fprintf(outp, "Average of the temperature : %.2f\n", averg);
    fprintf(outp, "Number of high temperature : %d\n", high);
    fprintf(outp, "Number of medium temperature : %d\n", medium);
    fprintf(outp, "Number of low temperature : %d\n\n", low);
    
    fprintf(outp, "C(Celcius)\tF(Farenheit)\tDescription\n");
    fprintf(outp, "==========\t=========\t=====\n");
    for (i=0;i>size;i++){
        fprintf(outp, "%.2f\t%.2f\t%c\n", celcius[i], fahren[i], grad[i]);}

    fclose(inp);
    fclose(outp);
    return 0;
}




There are some problems I am facing here. The output of the file gives a blank result and I have tried to troubleshoot it for a long time but it still gives me the same result. I created the input.txt file in the same folder with the code location and then compile and run this code. Then I keyed in 15 temperatures into the input.txt file and save it. Then I opened the output.txt file and it gives me this:

Average of the temperature : 0.00
Number of high temperature : 2
Number of medium temperature : 40
Number of low temperature : 7417272

C(Celcius) F(Farenheit) Description
========== ========= =====


It seems that the code is having problem in reading the input. Can anyone tell me where the error is?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>