I need to read this into a arrays.
123455 45 67 87 69 98
3456787 67 98 34 54 67
456778 87 76 89 76 34
344568 65 78 76 45 98
56789 98 76 45 34 54

Here is the code I have

void getGrades(double exam[], int studentId[])
{
  ifstream inFile;
  inFile.open("student.txt");

  for (int i = 0; i < 5; i++)
  {
    inFile >> studentId[i];

  for (int j = 0; j < 5; j++)
  {
    inFile >> exam[j];
  }
}
}

void printGrades(double exam[], int studentId[])
{
  for (int i =0; i < 5; i++)
  {
    cout << "student id" << studentId[i] << endl;

  for (int j =0; j < 5; j++)
  {
    cout << "grades" << exam[j] << endl;
  }
 }
 }

And here is the out put I get

student id123455
grades98
grades76
grades45
grades34
grades54
student id3456787
grades98
grades76
grades45
grades34
grades54
student id456778
grades98
grades76
grades45
grades34
grades54
student id344568
grades98
grades76
grades45
grades34
grades54
student id56789
grades98
grades76
grades45
grades34
grades54

So my loops are working in reading the student id's, I'm not seeing what I'm doing wrong for the nested loop.

Recommended Answers

All 3 Replies

Show all of your code, including variable definitions. This is not sufficient to tell you why your grades are not in the proper order. However, you have only one exam[] array, and you need one for each student, which is why each student has the same grades...

Create a Student class that contains an array of exam grades, and then create an array of students. The pseudo-code for this would be:

for each student
    read student id
    for each exam
        read student exam grade
    endfor
endfor
#include <iostream>
#include <fstream>
using namespace std;


void getGrades(double exam[], int studentId[]);
void printGrades(double exam[], int studentId[]);
void findLowest(double exam[], int studentId[]);
int main()
{


  int studentId[5];
  double exam[5];
  getGrades(exam, studentId);
  printGrades(exam, studentId);


}

void getGrades(double exam[], int studentId[])
{
  ifstream inFile;
  inFile.open("student.txt");
  for (int i = 0; i < 5; i++)
  {
    inFile >> studentId[i];

  for (int j = 0; j < 5; j++)
  {
    inFile >> exam[j];
  }
}
}

void printGrades(double exam[], int studentId[])
{
  for (int i =0; i < 5; i++)
  {
    cout << "student id" << studentId[i] << endl;

  for (int j =0; j < 5; j++)
  {
    cout << "grades" << exam[j] << endl;
  }

}
}

It is as I said - you need a separate array of exam grades associated with each student. You have only one exam array, so it will ONLY contain the last set of data that you read into it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.