Program using 2-D arrays, pointers, infile etc.

I have difficulty compiling my code. I don't know what is wrong or what I have used wrongly.

This is the Assignment:
1) create a data file of the following structure (each represents the test scores of a class):
the first two numbers are the number of students and the number of tests (integers), respectively.
Then each row represents the test scores of a student. Each student's scores is in different row (it
does not really matter if they are in different row or not, in fact.) Below is an example of an
input file. You do not need to send me your input file. I will test your program on several different
input files.

3 6
3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4


2) create a program that asks for an input file that has the structure
described in 1).
a) Read the data into a multidimensional (2-D) dynamic array of
floats (by means of pointer and new). And:
b) Compute and print the average (mean) test score for each student
(you might label the student by 1, 2, 3, etc.)
c) Then delete the pointer arrays by using delete function PROPERLY.


And this is what I have:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;


int main()
{


float **studentgrades;
int student, tests;
string filename;
ifstream infile;
int i,j;
float mean;


cout<<"Enter the input file name:"<<endl;
cin>>filename;

infile.open(filename.c_str());
if(infile.fail())
{
cout<"Input file opening failed.\n";
exit(1)
}


infile>>students;
infile>>tests;

studentgrades=new float*[students];
for(i=0;i<students;i++)
{
studentgrades[i]=new float[tests];
}


for(i=0;i<students;i++){
for(j=0;j<tests;j++){
infile>>studentgrades[i][j]
}
}



for(i=0;i<students;i++)
{
mean=0;
}

for(j=0;j<tests;j++)
{
mean+=studentgrades[i][j];
}

mean=mean/tests;
cout<<"\n student"<<i+1<<"test average:"<<mean;


for(i=0;i<student;i++)
delete studentgrades[i];
delete[] studentgrades;

cout<<"Press any key to continue...";
cin.ignore();
cin.get();cin.get();
return0;
}
you should edit your post and use code tags.

comment out all the code that doesn't compile, which is all of it, add one line at a time until your code will compile.

return0; should be return 0;
you are missing ; at the end of statements
you are missing {} around your if else statements
you have misspelled words such as student, students

1
2
cout<"Input file opening failed.\n";
exit(1)


should be

1
2
cout<<"Input file opening failed.\n";
exit(1);
Last edited on
Topic archived. No new replies allowed.