the code ends out of nowhere -1073741819 (0xC0000005)

1

When I enter different numbers in "studentNumber" and "testNumber" variables, the code closes at a specific place. No matter what value I give to the variables, the code stands in the second exam part of the 1st student.

Code terminates with this error "Process returned -1073741819 (0xC0000005) execution time: 11.105 s"

#include <iostream>

using namespace std;

int studentNumber=0;
int testNumber=0;
struct Student
{
string name,grade;
int studentNo,*testResults;
double average;
};

void getValue()
{
    cout << "Enter the number of students: ";
    cin >> studentNumber;
    Student Students[studentNumber];
    cout << "Enter the number of tests: ";
    cin >> testNumber;
    int Arr[testNumber];
    Students[0].testResults = Arr;

    for(int i=1; i<=studentNumber; i++)
    {
        cout<< "\n" << "Enter the name of the " << i << ". student: ";
        cin >> Students[i].name;
        cout<< "\n" << "Enter the number of the " <<  i << ". student: ";
        cin >> Students[i].studentNo;

        for(int z=0; z<testNumber; z++)
        {
            cout<< "\n" << "Enter the " << z+1 << ". exam grade of the " << i << ". student: " ;
            cin >> Students[i].testResults[z];
        }
    }
}

int main()
{
    getValue();
}

result:

picture of the problem here

c++
struct
asked on Stack Overflow Apr 18, 2021 by Emre Oz • edited Apr 18, 2021 by rustyx

1 Answer

0

Two issues:

This line:

for(int i=1; i<=studentNumber; i++)

Should absolutely be:

for(int i=0; i < studentNumber; i++)

The other issue is here:

    for(int z=0; z<testNumber; z++)
    {
        cout<< "\n" << "Enter the " << z+1 << ". exam grade of the " << i << ". student: " ;
        cin >> Students[i].testResults[z];
    }

Specifically this line:

cin >> Students[i].testResults[z];

Although you correctly assigned Studends[0].testResults to a valid location at the start of the program, you haven't assigned a pointer for testResults[z] yet when z > 0.

You need to allocate a testResults array for each student. And it appears you you need to learn about dynanmic allocations. (e.g. "new" and "delete").

Remove these two lines:

int Arr[testNumber];
Students[0].testResults = Arr;

And while we're add it, remove this line too, since it's non-standard for C++.

Student Students[studentNumber];

Then allocate a testResults array within each loop. And before the loop starts, correctly allocate the Students array:

Student* Students = new Student[studentNumber]; // ADD THIS LINE

for(int i=0; i< studentNumber; i++)
{
    cout<< "\n" << "Enter the name of the " << i << ". student: ";
    cin >> Students[i].name;
    cout<< "\n" << "Enter the number of the " <<  i << ". student: ";
    cin >> Students[i].studentNo;

    Students[i].testResults = new int[testNumber];  // ADD THIS LINE

    for(int z=0; z<testNumber; z++)
    {
        cout<< "\n" << "Enter the " << z+1 << ". exam grade of the " << i << ". student: " ;
        cin >> Students[i].testResults[z];
    }
}

I'm assuming you need to write code to compute the average test score. i'll leave that as an exercise for you.

But don't forget to release the memory allocations before your code exits:

for(int i=0; i< studentNumber; i++)
{
   delete [] (Students[i].testResults);
}
delete [] Students;
answered on Stack Overflow Apr 18, 2021 by selbie • edited Apr 20, 2021 by selbie

User contributions licensed under CC BY-SA 3.0