I.b. Stuck on function def

This is a discussion on I.b. Stuck on function def within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to write some code that will read in an array of test grades and calculate some ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    49

    I.b. Stuck on function def

    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
    Semper Fi!

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Three suggestions:

    1. You have a typedef that defines Table as a vector of TableRow objects, then you define a class called Table. You can't do both. Why do you need that class anyway?

    2. It looks like your main should be something simple like:
    Code:
    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";
    
    	Table data;
    	fill(INPUT_FILE, data);  // I GET LOST RIGHT HERE...!
    	print(cout, data);		
    }
    3. Inside your print function, you accidentally used cout instead of out to output the endl.

    I haven't tried your code or anything, I just noticed those things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21