#include <iostream>
#include <fstream>
#include <cstdlib>
usingnamespace std;
void get_average(ifstream& input_file);
// Precondition: Reads all the numbers from input file stream
// Postcondition: Prints out to the screen the average of the numbers
int main()
{
ifstream input_file;
cout << "Opening program to find the average of the numbers on the input file." << endl;
input_file.open("input.dat");
if (input_file.fail()) {
cout << "Opening input file failed." << endl
<< "Closing program now." << endl;
exit(1);
}
get_average(input_file);
input_file.close();
cout << "End of Program";
return 0;
}
void get_average(ifstream& input_file)
{
double sum=0;
double next;
int count=0;
input_file >> next;
while (input_file >> next) {
sum = sum + next;
count++;
input_file >> next;
}
cout << (sum/count);
}
if (input_file.fail()) {
cout << "Opening input file failed." << endl
<< "Closing program now." << endl;
If so this just means your .exe can't find the text file specified. Either try moving it to the same folder as the .exe or the project directory for whatever IDE you are using.
UPDATE: Installed Neatbeans and for the most part the program started to work. Now the function get_average is only grabbing every other number in my file... This should be fun.