tags:

views:

91

answers:

3

I think the problem is an access violation but I just can't seem to find it. Here is the function.

// Structure
struct TestScores
{
    char Name[40];  // Student name
    int Idnum;  // Student ID number
    double *Tests;  // Pointer to an array of test scores
    double Average;  // Average test score
    char Grade;  // Course grade
};

student is dynamically created and points to TestScores. In this function, let's say totalStudents is 2 and there are 5 tests. The access violation prompt screen shows up when it finishes one loop of one student for the test scores. Then it does one more question on what was the score for student 2 on test 1 but it stops right there.

// Gets ID numbers, names, and test scores of every student
void getId_Scores(TestScores *student, int totalStudents, int tests)
{
    int count;  // Counter

    // Loop will get student names
    for (count = 0; count < totalStudents; count++)
    {
     cout << "Enter student " << count + 1 << "'s name. ";
     cin  >> student[count].Name;
    }

    // Loop will collect ID numbers of all students
    for (count = 0; count < totalStudents; count++)
    {
     cout << "Enter " << student[count].Name << "'s ID number: ";
     cin  >> student[count].Idnum;

     // Input Validation
     while (student[count].Idnum < 0)
     {
      cout << "You entered a negative ID number. Enter again. ";
      cin  >> student[count].Idnum;
     }
    }

    /* Loop will collect test scores of all students.
    Big loop for number of students and small loop for 
    number of tests. */
    for (int studNum = 0; studNum < totalStudents; studNum++)
    {
     for (int testNum = 0; testNum < tests; testNum++)
     {
      cout << "Enter " << student[studNum].Name << "'s score on test ";
      cout << testNum + 1 << ": ";
      cin  >> student[studNum].Tests[testNum];

      // Input Validation
      while (student[studNum].Tests[testNum] < 0)
      {
       cout << "You entered a negative test score. Enter again. ";
       cin  >> student[studNum].Tests[testNum];
      }
     }
    }
}
A: 

Probably, you have not allocated the array of students correctly. Please check and post the calling code, including how you are creating the students, and tests.

1800 INFORMATION
+1  A: 

If instead of using dynamically allocated arrays, you use the std::vector class, and instead of operator[] you use the vector's at() method, you will get an exception at the exact bad vector access. You should also investigate the use of std::string instead of arrays of char.

PS As a matter of interest, which C++ text book are you learning from?

anon
Everyone and NeilWell, I figured the problem out but it was just that I wasn't creating a student.Tests. Instead, it was for one student.And I am using Tony Gaddis intro to c++
A: 

I was simply allocating student.Tests wrong. I was creating only one student test score array while I need more but thank you everyone.

ps. My book forces me to use arrays and the sort, I can't use vectors or anything else.

thanks again

Your book is setting you up for failure, then.
GMan
Eh, you should still learn and understand how arrays work in C++ even if you are going to be using STL containers almost all the time. Whether it's better to learn arrays *first* or not, however, is highly debatable.
Tyler McHenry
You will need to know how arrays work at some point. What you definitely do not need to know when starting C++ is how to allocate them dynamically! Any book that suggests such a thing is frankly not worth reading.
anon