Creating dynamic array from class objects.

Status
Not open for further replies.

Pocky Is God

Ars Legatus Legionis
24,457
Code:
class student
{ public:
student();
void classes_added();
void idnumber();
void input();
void output();
void totalcourses();
private:
    string firstname;
	string lastname;
    string idnumber;
	string totalcourses;
	
};
student::student()
{
	firstname="";
	lastname="";
	idnumber="";
	totalcourses="";
}

void student::input()
{cout<< "Enter the first name of the student: "<<endl;
cin>>firstname;
cout<<"Enter the last name of the student: "<<endl;
cin>>lastname;
}
void student::classes_added()
{    cout<<"What class number are you adding? "<<endl;

       cin>>idnumber;

Is what I have so far for the class student. But I need to also create an entry to take in class numbers; trouble is I am having issues picturing how to do so dynamically based on entry from the user. I would ideally like to ask the user "How many classes are you taking? and take

Code:
nt classSize( 0);
cin >>classSize;
int* myArray = new int[classSize];
if(myArray!=0)
{
 
}
else
{
  //Work with array....
  delete[] myArray;
}

But I don't know how to edit objects of the class based on the input from the user. Or if that is even possible.

Here is the entirety of what the teacher wants:

"Make a project, consisting of 3 modules: student.cpp, course.cpp amd register.cpp. Write source and header file for a program that registers students for courses. Design a class student, that stores the name of the student , the id number and array(vector) of all course numbers for which the student is resisted.Design a class course that stores the course number, and array(vector) of the id numbers of all students who are registered for this course. In the register.cpp implement functions that add and drop students and print course lists."
 
Pocky Is God":2mm1292r said:
But I don't know how to edit objects of the class based on the input from the user. Or if that is even possible.
Yes it's possible. You're already doing it for firstname and lastname. From what you've got it seems like you already know what to do. Maybe if you could post the code you tried which didn't do what you wanted we could point out the problem. You're already able to create and populate a dynamic array outside of a class, right? What is it about doing it on a class member that's giving you trouble?

Also, when I write code in Visual Studio 2010 at least 90% of the time it won't compile in Geany in linux using C++. It this an issue with libraries or is my code just sloppy and it causes GCC to give me errors?
Hard to say without knowing what errors you are getting. It could be the libraries or it could be differences in the compilers. One pretty common thing even in code that's pretty conformant is that headers in different implementations of the standard libraries may indirectly include different things, so some code may build with one because a needed header is indirectly included by coincidence, and fails when using a different implementation because that headers isn't included anywhere.


BTW, I'd avoid that 'myArray = new int[...]; ... delete[] myArray;' stuff. I gave an example in that other thread of what I think is a better way using a vector and it sounds like your teacher wants you to use a vector:
Code:
#include <vector>

int main() {
    std::vector<int> x;
    int user_entries = get_int();
    for(int i=0;i<user_entries;++i) {
        x.push_back(get_int());
    }

    for(int i=0;i<x.size();++i) {
        std::cout << x[i] << std::endl;
    }
}
 
Status
Not open for further replies.