Dynamic array problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackstormdragon
    New Member
    • Feb 2007
    • 32

    #1

    Dynamic array problems

    It seems pointers and dynamic arrays are giving me a hard time.
    Heres part of the assignment. We have to create a class named Student that has three member variables. One of the variables is called classList – "A dynamic array of strings used to store the names of the classes that the student is enrolled in"

    My problem is when Im trying to send classList from my input function to my setClassList function. I keep getting the error "subscript requires array or pointer type' and I have no clue what that means. Heres some pieces of my code.
    Code:
    typedef char* CharPtr;
    
    class Student
    
    {
    public:
    	~Student();
    	void input();
    	void output();
    	void setNumClasses(int);
    	int getNumClasses();
    	void setClassList(char);
    	char getClassList();
    	void setName(char);
    	char getName();
    	void reset();
    	
    		
    private:
    	char _name;
    	int _numClasses;
    	char _classList;
    };
    void Student::input()
    {
    	CharPtr classList;
    	classList = new char[];
    	int numberClasses;
    	char name;
    
    	cout<<"Enter students name ";
    	cin>>name;
    	setName(name);
    
    	cout<<"Enter the number of classes student is taking ";
    	cin>>numberClasses;
    	setNumClasses(numberClasses);
    
    	for(int counter = 0; counter < numberClasses; counter++)
    	{
    		cout<<"Enter name of class ";
    		cin>>classList[counter];
    		setClassList(classList[counter]);
    	}	
    
    void Student::setClassList(char classList)
    {
    	int index = 0;
    	_classList = classList[index];
    }
    Now this isnt all my code, these are just the snipets Im having problems with. Oh and the classList is a char because we havent gone over strings yet.
    Thanks.
    Last edited by AdrianH; Apr 27 '07, 08:35 PM. Reason: PLEASE use [code][/code] tags. It improves readability.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by blackstormdrago n

    void Student::setCla ssList(char classList)
    {
    int index = 0;
    _classList = classList[index];
    }
    In ur typedef u have char* CharPtr which is pointer.

    later in main u have CharPtr classList so this also will be a pointer.

    In ur function u are calling char classList which is simple char not pointer to char.

    Think about this

    Savage

    Comment

    • blackstormdragon
      New Member
      • Feb 2007
      • 32

      #3
      Originally posted by Savage
      In ur typedef u have char* CharPtr which is pointer.

      later in main u have CharPtr classList so this also will be a pointer.

      In ur function u are calling char classList which is simple char not pointer to char.

      Think about this

      Savage
      Thanks, you hint sent me in the right direction. I think I figured it out.

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by blackstormdrago n
        Thanks, you hint sent me in the right direction. I think I figured it out.
        Always a pleasure......

        Savage

        Comment

        Working...