Hi all, I am a Computer Science student who just started C++. I am on struct right now. I got an error message but I don't have any idea.. It says "segmentation fault" . I looked up what kind of error message this is and it says "caused by memory location that doesn't belong to you ". So I assume my last for loop has syntactical error but I can't pinpoint what's wrong and why is wrong.
followings are my code
struct Course
{
string name;
int Idnum;
int *test;
double average;
double grade;
};
int main()
{
Course *data1=nullptr;
int SIZE1;
int SIZE2;
cout<<"Enter the number of students in the group : ";
cin>>SIZE1;
data1 = new Course[SIZE1];
cout<<"Enter the number of test done by these students : ";
cin>> SIZE2;
data1[SIZE1].test =nullptr;
data1[SIZE1].test = newint[SIZE2];
cin.ignore();
for(int i=0; i<SIZE1; i++)
{
cout<<"Enter the name of student :";
getline(cin,data1[i].name);
cout<<"Enter the ID number of this student : ";
cin>>data1[i].Idnum;
for(int j=0; j<SIZE2; j++)
{
cout<<"Enter test#"<<i+1<< ": ";
cin>>data1[i].test[j];
}
There are two errors here.
Firstly, you are writing to offset SIZE1, which is one element beyond the end of the array. Remember that array offsets range from 0 to one less than the size.
Secondly, you need to assign this memory in a loop since you need to assign it to every student.
Also note, there's no need to set a pointer to nullptr just before you set it to something else.
#include <string>
#include <iostream>
struct Course {
std::string name;
int Idnum {};
int* test {};
double average {};
double grade {};
};
int main()
{
size_t SIZE1 {};
size_t SIZE2 {};
std::cout << "Enter the number of students in the group : ";
std::cin >> SIZE1;
std::cout << "Enter the number of test done by these students : ";
std::cin >> SIZE2;
constauto data1 {new Course[SIZE1]{}};
for (size_t i = 0; i < SIZE1; ++i) {
std::cout << "Enter the name of student :";
std::getline(std::cin >> std::ws, data1[i].name);
std::cout << "Enter the ID number of this student : ";
std::cin >> data1[i].Idnum;
data1[i].test = newint[SIZE2] {};
for (size_t j = 0; j < SIZE2; ++j) {
std::cout << "Enter test#" << j + 1 << ": ";
std::cin >> data1[i].test[j];
}
}
for (size_t i = 0; i < SIZE1; ++i) {
std::cout << data1[i].Idnum << '\t' << data1[i].name << '\t';
for (size_t j = 0; j < SIZE2; ++j)
std::cout << data1[i].test[j] << ' ';
std::cout << '\n';
}
for (size_t i = 0; i < SIZE1; ++i)
delete[] data1[i].test;
delete[] data1;
}
Thank you so much, I changed the code according to your advice. It seems working well although I need to spend more time to understand what is going on by these pointers. I will leave my modified code here.
for(int i=0; i<SIZE1; i++)
{
data1[i].test = newint[SIZE2];
cout<<"Enter the name of student :";
getline(cin,data1[i].name);
cout<<"Enter the ID number of this student : ";
cin>>data1[i].Idnum;
for(int j=0; j<SIZE2; j++)
{
cout<<"Enter test#"<<j+1<< ": ";
cin>>data1[i].test[j];
total = total +data1[i].test[j];
}
data1[i].average = total/SIZE2;
total =0;
cin.ignore();
}