well i have this program that i have written error free but my return keeps coming out wrong can anyone help me resolve this issue. I am including my source code.
Code:
#include <iostream>
#include <fstream> // I/O
#include <iomanip> // For setw()
using namespace std;
const int MAX_FILE_NAME = 35; // Maximum space allocated for file name
void open_input(ifstream& input, char name[]); // Get file name & Open file
double find_average(ifstream& input, double& average); // Find avg values
void output(const char name[], double find_average, ostream& os = cout); // Print results
int main()
// Parameters: None
// Returns: Zero
// Calls: open_input(), find_max_min(), 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 = 0.0;
double avag = 0;
// Maximum and minimum numbers from file
cout << "This program can find the average numbers in a file\n"
<< "of doubles.\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, average); // Find average values in file
input_numbers.close(); // Close file
output(file_name, avag);
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;
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()
double find_average(ifstream& input, double& average) // Find max & min values
// Parameters: Variables for file reference and max and min values
// Returns: None
// Calls: None
{
double value;
double sum = 0;
int count = 0;
while (input >> value) // Continue as long as we can read a number from file.
{
average = sum/count;
sum += value;
count = count + 1;
}
return sum/count;
} // End of average file
void output(const char name[], double average, ostream& os) // Print results
// Parameters: File name, max & min values from file, output stream
// Returns: None
// Calls: None
{ os << "\n\nInput your File Name Please : " << name << endl;
os << "Your average : " << setw(8) << average<< endl;
}