Strange intermittent and elusive math error in code

I have aced this C++ course until this module, and i have literally spent 3 days trying to find my error in this code. I give up, and am asking for help. With the following input files:
Class1.txt 95.6 78.3 88.8 46.7 98.5 67.1 84.0 70.3 82.4 73.6
CLass2.txt 95.6 78.3 88.8 46.7 98.5 67.1 84.0
Class3.txt 95.6 78.3 88.8 46.7 98.5
Class4.txt 70.0 80.0 90.0

The math is correct on class4.txt, but constantly incorrect on the other files. I know you are going to make me an idiot, but I have looked at it for so long I will never find it.

(I mean, obviously it is in void find_average)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Purpose:		A program that will take input from a file of numbers type double and output the average of the
//				numbers to the screen. Using a function that will receive input file stream as a parameter, 
//				read the file, find and return the average of the numbers.
//				Output file name and average
//				Allow to process multiple files in one run
//				

#include <iostream>
#include <fstream> // I/O
#include <iomanip> // For setw()
using namespace std;

ofstream outputfile("output.txt");
const int MAX_FILE_NAME = 35; // Maximum space allocated for file name

void open_input(ifstream& input, char name[]);          // Get file name & Open file
void find_average(ifstream& input, double& sum, double& count, double& solution); // Find average of grades
void output(const char name[], double solution, ostream& os = cout); // Display and file 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 sum, count, solution;                      // Average of grades from file

   cout << "This program will average the numbers in a file\n"
        << "of gradess.\n" << endl;
   system("pause"); // Hold message on screen until key is pressed

   do 
   {  
                                 // Clear screen
      open_input(input_numbers, file_name);    // Get file name & open file
      find_average(input_numbers, sum, count, solution);   // Find average of file values
      input_numbers.close();                   // Close file
      output(file_name, solution);             // Print results on screen
      output(file_name, solution, outputfile);    // Print results on outputfile

      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;
   outputfile << "\n\nThanks!\n"; 
   outputfile.close();
   
   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);// 
      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& sum, double& count, double& solution) // Find average of data
// Parameters: Variables for file reference and average values
// Returns:    None
// Calls:      None
{  
   double num = 0;              // Value from file
   sum = 0;
   count = 0;
   solution = 0;
   input >> num;         // Read first number
														      
   while (input >> num)  // Continue as long as we can read a number from file.
   {  
      sum=sum+num;
	  count++;
	  input >> num;
   }
   if (count > 0)
   {
	   solution=sum/count;       
	}	
} // End of find_max_min()

void output(const char name[], double solution, ostream& os) // Print results
// Parameters: File name, max & min values from file, output stream
// Returns:    None
// Calls:      None
{  os << "\n\nInput File Name: " << name << endl;
   os << "Grade Average : " << setw(8) << solution << endl;
  
} // End of output()
Remove lines 91 and 97. You're reading values but doing nothing with them.
Class4.txt worked because it turns out that the only value your code took into account for the computation of the average was equal to the average of the file.
You are awesome. I can sleep now.
Topic archived. No new replies allowed.