Hi, I am trying to write some code that will read in an array of test grades and calculate some averages. I thought I was doing fairly well until it came time to actually call the function that reads in the values from a text file. I got a dose of stupid from somewhere, and it has been mangling my brain for 6 hours now.
The problem lies right where it says fill(INPUT_FILE, WHAT GOES HERE) --- I've got no clue. I thought "aTable" should go there, but it reports it as an undeclared identifier. I then wrote a class for Table, and then promptly received a FATAL ERROR, Internal Compiler error. So I renamed the class to TableA for now, figuring that if I can get ahold of how to define that "fill"
function I can figure out the rest on my own.
So my basic question is: what argument do I use to replace WHAT_GOES_HERE so that I can call my fill function and get this dog hunting again. An explanation would be most appreciated.
here is the grades.cpp file
Code:
/* Grades.cpp
* Reads exam scores and finds the average for each student, and the average
* of all students for each exam. */
#include <iostream> // cout, cin, <<, >>
#include <fstream> // ifstream
#include <string> // string
#include <cassert> // assert()
#include "GradesA.h"
using namespace std;
int main()
{
const string INPUT_FILE = "grades.txt";
cout << "This program will calculate the average grade for each student.\n"
<< "And the average of the students for each exam.\n";
fill(INPUT_FILE, WHAT_GOES_HERE???? ); // I GET LOST RIGHT HERE...!
}
void print(ostream& out, const Table& aTable)
{
for (int row = 0; row < aTable.size(); row++)
{
for (int col = 0; col < aTable[row].size(); col++)
out << aTable[row][col] << '\t';
cout << endl;
}
}
void fill(const string& fileName, Table& aTable)
{
cout << "\n Hello from fill\n";
ifstream in(fileName.data() ); // open stream to file
assert(in.is_open()); // verify
int rows, // variables
columns; // for dimensions from first line of file
in >> rows >> columns; // read dimensions
Table locTable(rows, TableRow(columns)); // construct a local
// rows X columns table
for (int r = 0; r < rows; r++) // for each row
for (int c = 0; c < columns; c++) // input column values
in >> locTable[r][c]; // into row. Assign locTable
aTable = locTable; // to aTable so it has correct dimensions
in.close();
}
here is the gradesA.h file
Code:
/* GradesA.h contains the declarations for type Table */
#include <vector> // vector
#include <iostream> // istream, ostream
#include <string> // string
using namespace std;
typedef vector<double> TableRow;
typedef vector<TableRow> Table;
// ---output a Table via an ostream
void print(ostream& out, const Table& aTable);
// ---fill a Table with values from a file
// whose first line is the # of rows and columns
void fill(const string& fileName, Table& aTable);
class Table
{
public:
void print(ostream & out);
void read(istream & in);
private:
int myRow;
int myColumn;
};
istream& operator>>(istream& in, Table& aTable);
ostream& operator<<(ostream& out, const Table& aTable);
and this is the little text file containing the data to be read
Code:
3 3
95 100 95
90 85 95
100 100 80