If statement in while Loop

The code runs without an error, but the outfile shows that it isn't properly incrementing the values for temp_fail, press_fail, and dwell_fail, I can't seem to figure out why.
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
36
37
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#define FILENAME "suture.txt"

main()
{
      int numdata=0, temp_fail=0, press_fail=0, dwell_fail=0, batchnum=0;
      float temp, press, dwell;
      FILE *infile;
      FILE *outfile;
      outfile = fopen("Lynch_outfile.txt","w");
      infile = fopen(FILENAME, "r");
             if(infile == NULL)
                printf("Error opening input file.");        
             else
             {
                 while((fscanf(infile,"%lf %lf %lf %lf",&batchnum, &temp, &press, &dwell)==4))
                 {
                  if(temp<150 || temp>170)
                       temp_fail++;      
                  if(press<60 || press>70)
                       press_fail++;
                  if(dwell<2 || dwell>2.5)
                       dwell_fail++;
                  numdata++;        
                  }
                 fprintf(outfile,"Total defective batches:         %i\n", numdata);
                 fprintf(outfile,"Defective due to temperature:    %.3f (%.2f %)\n", temp_fail, (temp_fail/numdata)*100);
                 fprintf(outfile,"Defective due to pressure:       %.3f (%.2f %)\n", press_fail, (press_fail/numdata)*100);
                 fprintf(outfile,"Defective due to dwell time:     %.3f (%.2f %)\n", dwell_fail, (dwell_fail/numdata)*100);
                 fclose(infile);
                 fclose(outfile);
             }
      getch();
      return 0;
}


The infile "suture.txt" looks like
1
2
3
4
5
6
24551 145.5 62.3 2.13
24582 153.7 63.0 2.52
26553 160.4 58.8 2.51
26613 159.5 58.9 2.02
26624 160.5 61.5 1.98
27725 170.9 62.5 2.03


My current outfile "Lynch_outfile.txt" looks like
1
2
3
4
Total defective batches:         6
Defective due to temperature:    0.000 (0.00 )
Defective due to pressure:       0.000 (0.00 )
Defective due to dwell time:     0.000 (0.00 )
$ g++ -W{all,extra,pedantic}foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:18:89: warning: format ‘%lf’ expects argument of type ‘double*’, but argument 3 has type ‘int*’ [-Wformat=]
foo.cpp:18:89: warning: format ‘%lf’ expects argument of type ‘double*’, but argument 4 has type ‘float*’ [-Wformat=]
foo.cpp:18:89: warning: format ‘%lf’ expects argument of type ‘double*’, but argument 5 has type ‘float*’ [-Wformat=]
foo.cpp:18:89: warning: format ‘%lf’ expects argument of type ‘double*’, but argument 6 has type ‘float*’ [-Wformat=]
foo.cpp:29:120: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Wformat=]
foo.cpp:29:120: warning: format ‘%f’ expects argument of type ‘double’, but argument 4 has type ‘int’ [-Wformat=]
foo.cpp:29:120: warning: unknown conversion type character ‘)’ in format [-Wformat=]
foo.cpp:30:122: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Wformat=]
foo.cpp:30:122: warning: format ‘%f’ expects argument of type ‘double’, but argument 4 has type ‘int’ [-Wformat=]
foo.cpp:30:122: warning: unknown conversion type character ‘)’ in format [-Wformat=]
foo.cpp:31:122: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Wformat=]
foo.cpp:31:122: warning: format ‘%f’ expects argument of type ‘double’, but argument 4 has type ‘int’ [-Wformat=]
foo.cpp:31:122: warning: unknown conversion type character ‘)’ in format [-Wformat=]
Last edited on
Now I just feel silly :(
Thank you, Dev-C++ doesn't show all that.
Topic archived. No new replies allowed.