I'm working on a practice project where I have to take the input from the files type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks.
I pretty much have a way to input an output the file but I think I'm missing something. This is what I've done so far.
You not parsing or adding number anywhere for one...
Why do you need an output file?
Just use cin << num to get for numbers, it will parse them for you (assuming num is of type double.)
Hint. Declare an array to store the numbers in file and also a for loop to loop through the file ;) http://www.cplusplus.com/doc/tutorial/control/ that will get you going and make your program a bit shorter but more efficient
I have to take the input from the files type double and outputs the average of the numbers in the file to the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Example code */
ifstream dataFile("path/filename.extension"); // Open your data file for input use.
double sum = 0; // This is your running total.
int count = 0; // This is how many numbers have been read from the data file.
double number; // This is a temporary place for input to be stored.
while( dataFile >> number ) { // Try to get the next number from input, and continue to loop if successful.
count++; // You successfully got another number from the file so increment the count.
sum += number; // Also, add that number from the file to your running total.
}
double average = sum / count;