Problem with adding up numbers from a txt file
I'm having a big problem with adding up numbers from a .txt file. I figured out how to count up how many numbers there are in the file, but can't seem to figure out for the life of me how to add up all the numbers in the file for a grand sum. Here is my source code so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main ()
{
int high, low, temperature, int average;
int temperatureCount = 0;
char inFileName[PATH_MAX];
FILE *inFile;
puts("What file to open?");
gets(inFileName);
inFile = fopen(inFileName, "r");
if (inFile == NULL) {
printf("File could not be opened.\n");
system("pause");
exit(EXIT_FAILURE);}
while(fscanf(inFile, "%d", &temperature) == 1)
++temperatureCount;
fclose(inFile);
printf("\n\n%d temperatures read from file.\n\n", temperatureCount);
average = (temperature/temperatureCount);
printf("# daily temps : %d,\n", temperatureCount);
printf("Average temp: %d , \n", average);
printf("Highest temp: %d, \n", high);
printf("Lowest temp: %d, \n", low);
system("pause");
return 0;
}
I need to add up all the numbers so that I can get an average temperature. I also need to display the highest temperature and the lowest temperature from the file to the monitor, but have no clue how to do that either. Any help would be appreciated.