hey guys, i would greatly appreciate it if u wood help me out to see whats wrong with my codes.... heres my question and heres my code......
DONT 4GET TO MAKE THE TEXT FILE TO RUN IT !!!! <input.txt, output.txt> <fp_in, fp_out>
1. Write a program to average real numbers input from a text file called “input.txt”. Each line of the text file will contain a single number to be averaged. Collect and sum numbers from the file until there are no more numbers in the file, then compute the average. Write the sum, the number of entries in the file and the average to another text file called “output.txt”.
Create the following text file and use as your input file:
10
15
20
45
6
19
33
21
here are my codes:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
int main(int nNumberofArgs, char*psz[]){
FILE* fp_in;
FILE* fp_out;
int Average;
int sum = 0;
int count = 0;
int num;
fp_in = fopen("input.txt", "r");
fp_out = fopen("output.txt", "w");
//Error Checking
if (fp_in = NULL)
{
printf("File does not exist or could not be found/n");
system("PAUSE");
exit;
//return 0;
}
if (fp_out = NULL)
{
printf("File does not exist or could not be found/n");
system("PAUSE");
exit;
//return
}
fscanf(fp_in, "%d", &num);
while (num != EOF)
{
// printf("test2");
count++;
sum = sum + num;
fscanf(fp_in, "%d", & num);
}
Average = sum/count;
fprintf (fp_out, "The sum is equal %d", sum);
fprintf (fp_out, "the number of entries is %d", count);
fprintf (fp_out, "The average is %d", Average);
fclose(fp_in);
fclose(fp_out);
system("PAUSE");
return 0;
}