I have been working on this all evening, but still can't get the file to read in correctly. I know you guys probably hate beginners like myself taking up space on these bords, but I'm stumped.
Code:
/* Homework 6 */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define FILENAME "data1.dat"
int main()
{
/* declare variables */
int points=0;
int c1, c2;
double c3, ave, max, min, sum=0.0;
int flag0=0, flag1=0;
FILE *list;
/* open file */
list = fopen(FILENAME, "r");
if(list == NULL)
{
printf("error reading file \n");
}
else
{
/* read file */
while(!feof(list))
{
fscanf(list, "%i, %i, %lf", &c1, &c2, &c3);
/* compute the average, minimum and maximum */
if (c1 == 1)
{
printf("%i, %i, %lf \n", c1, c1, c3);
sum += c3;
points++;
printf("sum: %lf \n", sum);
printf("points: %i \n", points);
if (c2 == 0)
{
if (flag0 == 0)
{
flag0 = 1;
min = c3;
}
if (c3 < min) min = c3;
}
if (c2 == 1)
{
if (flag1 == 0)
{
flag1 = 1;
max = c3;
}
if (c3 > max) max = c3;
}
}
}
}
/* compute ave */
if(points == 0)
{
printf("error, cannot divide by zero \n");
}
else
{
ave=sum/points;
/* print a summary */
printf("average value: %7.2f \n", ave);
printf("maximum reading: %7.2f \n", max);
printf("minimum reading: %7.2f \n", min);
}
fclose(list);
return 0;
}
I also tried while((fscanf(list, "%i, %i, %lf", &c1, &c2, &c3)) == 3)
and while((fscanf(list, "%i, %i, %lf", &c1, &c2, &c3)) !=EOF)
with similar results. my conclusion is the file is not being read in correctly, I think It would return correct numbers if I could get it to read the file.
input file consists of:
data1.dat:
1 0 3.4
0 0 1.3
1 1 3.1
1 0 0.4
1 1 7.9
1 1 4.2
1 0 -2.1