Hey, Ive been looking around to find out how to read data into a 2D array and I cant find a solution that will fit my assignment. Im not sure that I should even use an array actually...
ok so there is a text file set up like this
21909 87 98 95 96 79
21910 74 75 84 86 57
21911 84 85 82 80 79
etc... There is no defined number of items in the file. (my professor wants to be able to read in any number of "rows" but there will always be 6 columns- "student ID" and 5 imaginary test scores.
Then we need to process the data and find averages and letter grades for each student, and a class average. I thought the easiest thing would be to use a 2D array. Here is what I have so far:
#include <iostream>
#include <fstream>
using namespace std;
int number;
// Function Prototypes
bool checkGrade(int);
int main()
{
int counter = 0;
ifstream inputFile;
inputFile.open("grade.txt");
if (!inputFile)
cout << "Error opening file!" << endl;
else
{
// determines number of items in input file.
int item;
while (!inputFile.eof())
{
inputFile >> item;
counter++;
}
const int rows = (counter/6); // determine number of rows in array.
int data[rows][6]; // declare 2D array for data in file. // 33!
// write data into 2D array
for (int indexrow=0; indexrow<rows; indexrow++)
{
for (int indexcol=0; indexcol<6;indexcol++)
{
inputFile >> data[indexrow][indexcol];
}
}
// process data in array
for (int indexr=0; indexr< rows; indexr++)
{
for (int indexc=0; indexc < 6; indexc++)
{
number = data[indexr][indexc];
checkGrade(number);
}
}
}
}
bool checkGrade(int num)
{
bool status;
if (num >= 1 && num <= 100)
status = true;
else
status = false;
return status;
}
When I build I get 3 errors:
-expected constant expression
-cannot allocate an array of constant size 0
-'data': unknown size
all on line 33