dynamically allocates memory for each new element in the array and copies it to the passed array. I usually would find the answer sooner or later but this one is seriously giving me a problem.
When the code executes, I'm able to put all of the input in but then it will crash I think when it returns to main.
here is the code:
#include<iostream>
#include<string>
using namespace std;
struct Class //Class structure
{
string title;
int units;
char grade;
};
struct Student //Student structure
{
string name;
double gpa;
Class classes[10];
};
int read(Student**, int, int); //Function that reads the input and fills the students array; returns number of students inputted
int main()
{
const short S_SIZE = 100,C_SIZE = 10; //Size of max amount of students, Size of max amount of classes
int n_size = 0;
Student *students[S_SIZE] = {NULL}; // Initializing 100 Student pointers set to NULL
n_size = read(students,S_SIZE,C_SIZE);
for(int i = 0; i < n_size; i++)
{
cout<<students->name<<endl<<endl; //Just to see if it worked right
}
system("pause");
}
int read( Student** students, const int S_SIZE, const int C_SIZE) //The bulk of code is just so when you press enter it will mean you're done
{
bool quit = false;
int count = 0,csub = 0;
Student *temp[100] = {NULL};
for(int i = 0; i < S_SIZE; i++)
{
do
{
temp = new Student;
cout<<"\n\nPlease Enter Student's full Name: ";
getline(cin,temp->name);
if(temp->name!="")
{
do
{
cout<<" \nPlease Enter Class Name: ";
getline(cin,temp->classes->title);
if(temp->classes->title != "")
{
cout<<"\nEnter units for "<<temp->classes->title<<": ";
cin>>temp->classes->units;
cout<<"\nEnter Grade for "<<temp->classes->title<<": ";
cin>>temp->classes->grade;
cin.ignore();
csub++;
}
}while(temp->classes->title != "" && csub < 10);
count++;
}
else
quit = true;
}while(quit == false);
if(quit)
break;
}
for(int i = 0; i < count; i++)
{
students = temp;
}
delete [] temp;
return count;
}
Any Help is greatly appreciated
#include<iostream>
#include<string>
using namespace std;
struct Class //Class structure
{
string title;
int units;
char grade;
};
struct Student //Student structure
{
string name;
double gpa;
Class classes[10];
};
int read(Student**, int, int); //Function that reads the input and fills the students array; returns number of students inputted
int main()
{
const short S_SIZE = 100,C_SIZE = 10; //Size of max amount of students, Size of max amount of classes
int n_size = 0;
Student *students[S_SIZE] = {NULL}; // Initializing 100 Student pointers set to NULL
n_size = read(students,S_SIZE,C_SIZE);
for(int i = 0; i < n_size; i++)
{
cout<<students->name<<endl<<endl; //Just to see if it worked right
}
system("pause");
}
int read( Student** students, const int S_SIZE, const int C_SIZE) //The bulk of code is just so when you press enter it will mean you're done
{
bool quit = false;
int count = 0,csub = 0;
Student *temp[100] = {NULL};
for(int i = 0; i < S_SIZE; i++)
{
do
{
temp = new Student;
cout<<"\n\nPlease Enter Student's full Name: ";
getline(cin,temp->name);
if(temp->name!="")
{
do
{
cout<<" \nPlease Enter Class Name: ";
getline(cin,temp->classes->title);
if(temp->classes->title != "")
{
cout<<"\nEnter units for "<<temp->classes->title<<": ";
cin>>temp->classes->units;
cout<<"\nEnter Grade for "<<temp->classes->title<<": ";
cin>>temp->classes->grade;
cin.ignore();
csub++;
}
}while(temp->classes->title != "" && csub < 10);
count++;
}
else
quit = true;
}while(quit == false);
if(quit)
break;
}
for(int i = 0; i < count; i++)
{
students = temp;
}
delete [] temp;
return count;
}