CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2009
    Posts
    146

    Unhappy 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!

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Dynamic array in a class Help

    but it creates an error
    Doesn't mean that it is the right way to do it. What error are you getting ?

  3. #3
    Join Date
    Jan 2009
    Posts
    146

    Re: Dynamic array in a class Help

    Quote Originally Posted by Skizmo View Post
    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!

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  5. #5
    Join Date
    Jan 2009
    Posts
    146

    Re: Dynamic array in a class Help

    Quote Originally Posted by Lindley View Post
    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!

  6. #6
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    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

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Dynamic array in a class Help

    Quote Originally Posted by Elite Commando View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured