Average Output Code Help

I am having a problem getting the correct output. When I type in the first file, it gives me the average of -1.#IND for class 1. Can anyone help me find out what I'm doing wrong? Here's what I'm supposed to be doing:



Write a program that will take input from a file of numbers of type double and output the average of the numbers in the file to the screen.

Use a function that receives an input file stream as a parameter, reads the file, finds and returns the average of the numbers in the file.

Output the file name and average.

Allow the user to process multiple files in one run.

Assume this program will average exam grades for each of several classes.

Create the test data files before running your program and give them the names specified.

Use the following test data to test your program:


File name: :Values

1. class1.txt | 95.6 78.3 88.8 46.7 98.5 67.1 84.0 70.3 82.4 73.6

2. class2.txt | 95.6 78.3 88.8 46.7 98.5 67.1 84.0

3. class3.txt | 95.6 78.3 88.8 46.7 98.5

4. class4.txt | 70.0 80.0 90.0

Here's the code I created:

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
#include <iostream>
#include <fstream> // I/O
#include <iomanip> // For setw()
using namespace std;

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

void open_input(ifstream& input, char name[]);          // Get file name & Open file
void read_values(ifstream& input, double v[], int size, int& used); // Read values
double find_average(ifstream& input, double& average); // Find average values
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 = 0.0;
   
   cout << "This program can find the average numbers in a file\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 

	 output(file_name, average);
	 output(file_name, average, outputfile); // and outputfile
	  
      
	  input_numbers.close();                   // Close file
      

      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 for using Averages!\n"; 
   outputfile.close();
   
   return 0;

   cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
}  // 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 and min values
// Parameters: Variables for file reference and max and min values
// Returns:    None
// Calls:      None
{  
   double avag;              
   double sum = 0;
   int count= 0;
   
   while (input >> avag)  // Continue as long as we can read a number from file.
   {  
	sum += avag;
	count = count + 1;
   }
   average = sum/count;
   return sum/count;
} // End of Average file


void output(const char name[], double average, ostream& os) // Print results
// Parameters: File name, max and 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;


}
Last edited on
I suggest starting simpler. Start by opening one file that you know exists (don't bother asking the user for a file name, just supply the name to the stream constructor). Read the contents of that file, printing each value as you read them. Compute the sum, printing it to the screen. Compute the average, print it to the screen. Once you are able to read that one file correctly and compute the average you can then ask the user for a file name, then repeat the above steps. Once you have this working correctly you can install your loop to do multiple files.

Topic archived. No new replies allowed.