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

rustyx
  • 80,671
  • 25
  • 200
  • 267
Emre Oz
  • 45
  • 5
  • Arrays in C++ start from 0. Either change `for(int i=1; i<=studentNumber; i++)` to `for(int i=0; i – rustyx Apr 18 '21 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 '21 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 '21 at 18:54

1 Answers1

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;
selbie
  • 100,020
  • 15
  • 103
  • 173
  • Wait what? that's not right... Each student should have it's own array vector of grades. – JHBonarius Apr 18 '21 at 18:53
  • @JHBonarius - I just noticed `for(int i=1; i<=studentNumber; i++)`. I'll update shortly – selbie Apr 18 '21 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 '21 at 18:59
  • @selbie I did but gave such an error error: 'Studends' was not declared in this scope| – Emre Oz Apr 18 '21 at 19:12
  • @selbie can you help me ? – Emre Oz Apr 19 '21 at 14:49
  • Sounds like `Studends` is misspelled. Maybe you can correct the spelling. – selbie Apr 19 '21 at 19:15
  • @selbie It's time to write your void "delete". I delete "Students [i] .testResults" as you gave, but is there any way to delete other variables (name, studentNo etc.) – Emre Oz Apr 20 '21 at 14:16