/********************************************
This program reads in grades from grades.txt,
which has 10 homework grades for 10 students.
The user then can query the program to
determine specific student/hw grades.
********************************************/
#include <fstream>
#include <string>
using namespace std;
/*******************************************
** PROTOTYPES
*******************************************/
int** getarray(int rows, int cols);
void readarray(int** A, int rows, int cols, istream& IN);
void query(int** A, int rows, int cols);
char getcommand();
/*******************************************
** MAIN
*******************************************/
int main()
{
// Read and store grade info
int **grade = getarray(10,10);
ifstream fin("grades.txt");
readarray(grade,10,10,fin);
// Process user queries
do {
query(grade,10,10);
}while(getcommand() != 'Q');
return 0;
}
/*******************************************
** DEFINITIONS
*******************************************/
// Create a rows x cols array of int's
int** getarray(int rows, int cols)
{
int** A = new int*[rows];
for(int i = 0; i < 10; i++)
A[i] = new int[cols];
return A;
}
// Read int values into rows x cols array A
void readarray(int **A, int rows, int cols, istream& IN)
{
for(int s = 0; s < rows; s++)
for(int g = 0; g < cols; g++)
IN >> A[s][g];
}
// Read and process query
void query(int **grade, int rows, int cols)
{
int sn, hw;
cout << "Student number [0..9]: ";
cin >> sn;
cout << "Homework number [0..9]: ";
cin >> hw;
cout << "Grade was " << grade[sn][hw] << endl;
}
// Get user's next command
char getcommand()
{
cout << "(Q)uit or (V)iew record? ";
char c;
cin >> c;
return c;
}