Hey everyone this my first post but I am having trouble with an assignment for my C++ class. I was suppose to calculate the average of 4 class from 4 different files. The program runs but I keep getting different averages for my output.
// Student: Mickey Wilson
// Assignment: Assignment 5
// File: assign 5.cpp
// Purpose: To take the student exam grades listed in a file
// then return the average of the exam as output.
// Input: Student exam grades from each class. class1.txt class2.txt
// class3.txt class4.txt
#include <iostream>
#include <fstream> // I/O
#include <iomanip> // For setw()
using namespace std;
const int MAX_FILE_NAME = 11; // Maximum space allocated for file name
void open_input(ifstream& input, char name[]); // Get file name & Open file
void find_average(ifstream& input, double& value, double& average, double sum, int& count); // Find average value
void output(const char name[], double average, ostream& os = cout); // Print results
int main()
// Parameters: None
// Returns: Zero
// Calls: open_input(), find_average(), output()
{ char again; // Does user want to go through loop again?
char file_name[MAX_FILE_NAME + 1]; // Name of file to be processed
ifstream input_numbers; // For working with input file
double average; // The class average from file
double value;
double sum = 0.0; // Value from file
int count= 0;
cout << "This program can find the average of the exams\n"
<< "of the students from each class.\n" << endl;
system("pause"); // Hold message on screen until key is pressed
do
{
system("cls"); // Clear screen
open_input(input_numbers, file_name); // Get file name & open file
find_average(input_numbers, value, average, sum, count); // Find the class average
input_numbers.close(); // Close file
output(file_name, average); // Print results on screen
cout << "\nDo you want to process another file (Y/N)? ";
cin >> again;
cin.ignore(256, '\n'); // Remove Enter key from keyboard buffer
} while ( again == 'y' || again == 'Y');
cout << "\nEnd of Program!" << endl;
cout << "\nThanks for using Class Average Caculator!" << endl;
return 0;
} // End of main()
void open_input(ifstream& input, char name[]) //Open file, exit on error
// Parameters: Variables for input file reference and input file name
// Returns: None
// Calls: None
{ int count = 0; // Count number of tries
do // Continue until we get a valid file name and can open file
{
count++;
if (count != 1) // Issue error message if we are trying again.
{ cout << "\n\aInvalid file name or file does not exist. Please try again."
<< endl;
}
cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME
<< " characters please)\n:> ";
cin.get(name, MAX_FILE_NAME + 1);// Gets at most MAX_FILE_NAME characters
cin.ignore(256, '\n'); // Remove Enter key from keyboard buffer
input.clear(); // Clear all error flags, if any, from prev try
input.open(name, ios_base::in); // Open only if file exists
} while (input.fail() ); // If can't open file, try again
} // End of open_input()
void find_average(ifstream& input, double& value, double& average, double sum, int& count) // Find average
// Parameters: Variables for file reference and class values
// Returns: None
// Calls: None
{
input >> value; // Read first number
while (input >> value) // Continue as long as we can read a number from file.
{
count++;
sum = sum + value;
average = sum / count;
}
} // End of find_average
void output(const char name[], double average, ostream& os) // Print results
// Parameters: File name, average values from file, output stream
// Returns: None
// Calls: None
{ cout << "\n\nInput File Name : " << name << endl;
cout << "The average for this class is : " << setw(8) << average << endl;
} // End of output()
void find_average(ifstream& input, double& value, double& average, double sum, int& count) // Find average
// Parameters: Variables for file reference and class values
// Returns: None
// Calls: None
{
// initialise sum and count
sum = 0.0;
count = 0;
// input >> value; // Deliberately discard first value
while (input >> value) // Continue as long as we can read a number from file.
{
count++;
sum = sum + value;
// average = sum / count; // No need to repeatedly re-calculate every time
}
average = sum / count; // Just get average once at the end.
} // End of find_average
Line 10 discards the first value from the file, this will give incorrect results.
Calculating the average within the loop at line 16 may give the correct result, but is inefficient. Move it outside the loop.
Lines 7 and 8 added - if the function is called more than once, these values need to be set to zero. It seems better to make this self-contained rather than depending on the calling function to do it.