Hi all,

So I'm creating a program that will calculate the grade average of students in a class.
I'm suppose to read a file into an array and I'm having problems with that.
Here is part of my code.

//Output Func
void StudentStat::Output()
{
	for (int i = 0; i < Size; i++)
	{
		cout << " " << " " << " ";
		for (int j = 0; j< Size; j++)
		{
			cout << " " << Names[i] << " " << Scores[j] << endl;
		}
	}
}

//Input Func
void StudentStat::Input(int val[])
{
	Size = val[0];
	int count = 1;

	for(int i = 0; i < Size; i++)
		for(int j = 0; j < Size; j++)
		{
			Names[i] = val[count++];
			Scores[j] = val[count++];
		}
}

//and here is the main
ifstream ProgEven ("bababa.dat");



int main()
{
	int temp[20];											//Values not set to null 
	int sqsize;
	StudentStat *Msq = new StudentStat();
	while (!ProgEven.eof())
	{
		ProgEven >> temp[0];								//The size is at temp[0];
		sqsize = temp[0];
		sqsize *= sqsize;
		for (int i = 0; i < sqsize; i++)
			ProgEven >> temp[20];
			Msq->Input(temp);	
			Msq->Output();
	}

return 0;

}

Thanks for the help

Oh and by the way, it compiles but it doest not show any of the names and scores of the students like in the dat file.

int temp[20];
ProgEven >> temp[20];

The second line overreads the end of the array. You are trying to put information in the 21st element of temp, but temp can only hold 20 elments because that's how many you declared in the first line. The largest valid index of and element in temp is 19.

Beyond that I can't figure out what you are trying to do. If the above doesn't fix things, then please comment your code or at least give a narative along the lines of:

The file holds grades for X number of students. Each student has Y number of scores. I need to find the average score for each student and the average score of all students.

Or whatever.

Oh, and don't use the return value of eof() as the conditional in your while loop. Use some sort of an input statement there. Exactly what to use may change depending on what you are trying to do.

Well I guess I did everything wrong than.
Coz I changed the value in temp to a higher number and it still didnt work.

And lets say that I have 5 students and 1 score for each student, than the file will look like this:

Jose 20
Bob 80
Linda 50
Joe 90
Maria 100

Just an example.

WARNING: the following code has not been compiled or tested. It is meant as an example to satisfy the criteria posted and demonstrate my earlier comments.

/*Read an student information regarding an unknown number of students from a file and calculate the average score of all students found.  File format contains a string without spaces representing a student name followed by a space and an integer representing a student score on a single line*/

struct StudentStat
{
   string name;
   int score;
};

int main()
{
   //declare input stream and associate with file
   ifstream fin("babadba.dat");

   //confirm file opened for reading
   if(!fin)
   {
     cout <<"file didn't open" << endl;
     cout << "exiting program" << endl;
     exit(1);
   }
   
   //declare variables needed 
   StudentStat student;
   int numStudents = 0;
   long totalOfAllScores = 0;

   //while can read a student name
   while(fin >> student.name)
   {
       //read a student score
       fin >> student.score;

       cout << student.name << " had score of " << student.score << endl;

       ++numStudents; 
       totalOfAllScores  +=  student.score;
    }
    
     if(fin.eof())
        cout << "file read in entirety without problem" << endl;
     else 
         cout << "corrupt file contents.  Unable to read full file contents" << endl;

    double avgScore = totalOfAllScores/numStudents;
     cout << "average score of all students was " << avgScore << endl;
   
    return 0;
}

Yeah, but instead of a struct, it was given a class StudentStats, and also the values are supose to be stores into an array.
so it would have to be
int number[]
string name[]

here is the *.h

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

class StudentStat
{
	private:
		int Size;
		string Names[20];
		int Scores[20];
		float Avg,
			  StDev,
			  Median;
		int   High_Score,
  			  Low_Score,
			  Range; 
		void Sortem ();
			
		public:
			void Input(int val[]);
			void Calc_Avg();
			void Calc_StDev();
			void Calc_Median();
			void Calc_Range();  // also calculates the high and low score
			void Output();
};

thanks

The logic remains the same, but adjust the code I posted to use arrays as member variables instead of indivual objects as member variables and adjust the code as needed. Here's one way to get started.

//while can read a student name
 while(fin >> students.names[numStudents])
 {
     //read a student score
     fin >> students.scores[numStudents];
This article has been dead for over six months. Start a new discussion instead.