-
April 15th, 2009, 08:27 AM
#1
Dynamic array in a class Help
I have created a dynamic character array as a private member of a class, now I want to take 5 inputs in the first 5 slots, I can't figure out how its done, here's my code:
Code:
void student::setcourse()
{
for(int i=0; i<5; i++)
{
cout<<"Enter course: "<<endl;
cin.getline(course,20,'\n');
}
}
I cannot figure out how Im going to enter the name of the courses in the first 5 slots of the "course" array, I've tried writing it like this: cin.getline(course[i],20,'\n'); but it creates an error (I'm using am MS Visual Studio Team System, Team suite 2008), please help, thanks!
-
April 15th, 2009, 08:46 AM
#2
Re: Dynamic array in a class Help
Doesn't mean that it is the right way to do it. What error are you getting ?
-
April 15th, 2009, 08:53 AM
#3
Re: Dynamic array in a class Help
Originally Posted by Skizmo
Doesn't mean that it is the right way to do it. What error are you getting ?
Alright, I changed my code now; and declared a 2D array as a private member like this:
char **course;
Allocated it memory in the constructor and my new code is:
Code:
void student::setcourse()
{
for(int i=0; i<5; i++)
{
cout<<"Enter course: "<<endl;
cin.getline(course[i],20,'\n');
}
}
The input problem is gone, but now I get a runtime error which says: cannot parse the function AT(*char) and I have no clue about what "parse" or "AT(*char)" is...
Here's the code in main:
Code:
int _tmain(int argc, _TCHAR* argv[])
{
student l; //student is the name of the class
l.setcourse();
cout<<endl;
return 0;
}
Thanks!
-
April 15th, 2009, 10:34 AM
#4
Re: Dynamic array in a class Help
char **course;
Allocated it memory in the constructor and my new code is:
You'd better be very specific about how you allocated it memory. That one is easy to screw up.
Why not just make your private member be
Code:
std::vector<std::string> course;
? That would make everything far simpler to deal with safely.
-
April 15th, 2009, 03:08 PM
#5
Re: Dynamic array in a class Help
Originally Posted by Lindley
You'd better be very specific about how you allocated it memory. That one is easy to screw up.
Why not just make your private member be
Code:
std::vector<std::string> course;
? That would make everything far simpler to deal with safely.
course = new char*[20];
That's how I allocated memory, as soon as I enter the FIRST course name during runtime, an error occurs.
std::vector<std::string> course;
I haven't learned these codes yet so Im using the ones I know. I really want to know whats causing the error. Thanks!
-
April 15th, 2009, 03:13 PM
#6
Re: Dynamic array in a class Help
Code:
course = new char*[20];
You have only allocated space for 20 pointers.
You should allocate space for the strings too.
Kurt
-
April 15th, 2009, 05:49 PM
#7
Re: Dynamic array in a class Help
Originally Posted by Elite Commando
course = new char*[20];
That's how I allocated memory, as soon as I enter the FIRST course name during runtime, an error occurs.
std::vector<std::string> course;
I haven't learned these codes yet so Im using the ones I know.
If that's the case, you're being taught 'C' instead of C++. If you really knew how to handle char**, you wouldn't have made such a simple mistake.
What does the book that you're using say about creating 2D arrays from a char**?
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|