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

3
  • Arrays in C++ start from 0. Either change for(int i=1; i<=studentNumber; i++) to for(int i=0; i<studentNumber; i++) or change Students[i] everywhere to Students[i-1] (there are more issues though).
    – rustyx
    Apr 18, 2021 at 18:50
  • In C++, arrays of size n are indexed between arr[0] to arr[n-1]. Accessing arr[n] is undefined behavior (also known as there be dragons here).
    – Eljay
    Apr 18, 2021 at 18:53
  • @rustyx thank you it worked. Do I need to close the topic? I don't know the rules of the site very much
    – Emre Oz
    Apr 18, 2021 at 18:54

1 Answer 1

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;
7
  • Wait what? that's not right... Each student should have it's own <s>array</s> vector of grades.
    – JHBonarius
    Apr 18, 2021 at 18:53
  • @JHBonarius - I just noticed for(int i=1; i<=studentNumber; i++). I'll update shortly
    – selbie
    Apr 18, 2021 at 18:54
  • @selbie I tried not to make another Arr but I don't know much about c ++. The variables in struct cannot change the constant in the assignment given to me.Couldn't manage to do it because "*testresults" is ptr
    – Emre Oz
    Apr 18, 2021 at 18:59
  • @selbie I did but gave such an error error: 'Studends' was not declared in this scope|
    – Emre Oz
    Apr 18, 2021 at 19:12
  • @selbie can you help me ?
    – Emre Oz
    Apr 19, 2021 at 14:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.