-1

I need to write a function that reads from a file and store the values into parallel arrays. In the text file I have a name and on the next line I have a set of 4 scores. Any tips on how to achieve this. here is an example of the text file

joe
30 75 90 88
ben 
100 75 93 20

now here is the code I have so far

ifstream input;

int main()
 {
string nameArray[];
double grades[];
void nameGrade(string[], double[]);

input.open("scores.txt");

nameGrade(nameArray, grades);

for (int i = 0; i <4; i++)
{
    cout << "student name: " << nameArray[i] << " Student Grades: " << grades[i] << endl;
}
input.close();
return 0;
}

void nameGrade(string name[], double grade[])
{
    for (int i = 0; i < 5; i++)
    {
        getline(input,studentName[i]);
        input >> studentGrade[i];
    }
}
BrokenCodez
  • 53
  • 1
  • 8
  • 2
    Each student has 4 grades, thus using the same index, `i` isn't going to work, and neither will "parallel arrays". It is not a one-two-one correspondence between the number of students and number of grades. – PaulMcKenzie Apr 18 '16 at 15:12
  • @PaulMcKenzie That's not true at all, all that has to be done is the file read into the array and then the elements added, you can start an array where you need to... Ie, if joe has the first 4 then 0 - 3 and so on.. You'd just have to break into multiple loops. Also op How are you determining who gets what grades? – suroh Apr 18 '16 at 15:16
  • Can you use STL containers or you are required to implement the arrays yourself? This would be a walk in the park with a `std::map >` to mention a quick example. – Nacho Apr 18 '16 at 15:17
  • @Afflicted When I see "parallel arrays", I assume that you have `m` things, and each thing contains `n` items. So `thing[0]` and `thing2[0]` are related, not `thing[0]` being related to `thing2[4]` or some other number. – PaulMcKenzie Apr 18 '16 at 15:21
  • @afflicted The 4 grades below the name correspond with the first name, I I need to total the 4 grades and store and then store that number in the corrosponding grade location – BrokenCodez Apr 18 '16 at 15:21
  • @BrokenCodez Forget the student name for a moment. Do you know how to read in 4 numbers and get a total of those numbers? That's the first thing you need to be able to do. – PaulMcKenzie Apr 18 '16 at 15:29
  • @ PaulMcKenzie sorry I didn't even see "parallel" only saw arrays : / – suroh Apr 18 '16 at 15:58
  • @Afflicted that seems to be my main issue right now is reading those 4 numbers and getting the total of them – BrokenCodez Apr 18 '16 at 17:02

1 Answers1

1

Since there is only 1 name per record and multiple grades, per record, move the reading of the name before the for loop:

void nameGrade(string& name,
               double grade[])
{
    getline(input,name);
    for (int i = 0; i < 5; i++)
    {
        input >> studentGrade[student_index * 5 + i];
    }
}

There is a complication in your design. Each student has more than one grade. Thus to handle this relationship, you will need either a 2 dimensional array of students or an array of structures:

struct Student_Name_Grades
{
  std::string name;
  std::vector<double> grades; // substitute array here if necessary.
};
std::vector<Student_Name_Grades> student_info;
// Or:  Student_Name_Grades student_info[MAXIMUM_STUDENTS];

Another alternative is to have 5 times as many grades slots as there are students. So to access the grades of student #2:

const unsigned int GRADES_PER_STUDENT = 5;
unsigned int student_index = 2;
double grade_1 = grades[student_index * GRADES_PER_STUDENT + 0];
double grade_2 = grades[student_index * GRADES_PER_STUDENT + 1];
double grade_3 = grades[student_index * GRADES_PER_STUDENT + 2];
double grade_4 = grades[student_index * GRADES_PER_STUDENT + 3];
double grade_5 = grades[student_index * GRADES_PER_STUDENT + 4];
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154