Dynamic Char array problem

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

    Dynamic Char array problem

    Im having trouble with my classList variable. It's a dynamic array of strings used to store the names of the classes(my program ask for students name, number of classes, then a list of the classes).

    Code:
    typedef char* CharPtr;
    
    class Student
    {
    public:
    	Student& operator =(const Student& rightside);
    	~Student();
    	void input();
    	void output();
    	void setNumClasses(int);
    	int getNumClasses();
    	void setClassList(char []);
    	char getClassList();
    	void setName(string);
    	char getName();
    	void reset();
    	
    		
    private:
    	string _name;
    	int _numClasses;
    	char *_classList;
    };
    void main()
    {
    	Student classInfo;
    	
    	classInfo.input();
    	
    
    }
    void Student::input()
    {
    	CharPtr classList;
    	classList = new char[];
    	int numberClasses;
    	string name;
    
    	cout<<"Enter students name ";
    	cin>>name;
    	setName(name);
    
    	cout<<"Enter the number of classes student is taking ";
    	cin>>numberClasses;
    	setNumClasses(numberClasses);
    
    	for(int index = 0; index <  numberClasses; index++)
    	{
    		cout<<"Enter name of class ";
    		cin>>classList[index];
    	}
    	setClassList(classList);
    
    }
    Everythinng works till I type in a class. If I type one letter, such as "a", everything is fine. Yet, if I type say "Math" then hit enter. It will print "Enter name of class" and then "press any key to continue". It wont let me type another class. I figured my problem is in the input() finction. I mean I think the problem is that each letter takes up an index space, but I'm just now learning about strings and dynamic arrays so I dont know what a good solution could be.
    Thanks.
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Originally posted by weaknessforcats
    \r is carriage return
    \n is carriage return + newline feed

    Probably you want \n.
    If you want to get technical, in reading from/writing to a text file you are correct... under Windoze. It is not portable though.

    Different operating systems define it differently. IIRC, Unix defines \n as carriage return only, Mac defines it as line feed + carriage return (reverse of Windows), other OSs I haven't a clue. I no longer work with text files anymore because of this portability issue, unless I am sure that the file generated will never be shifted to another OS or I don't care ;).


    Adrian
    Originally posted by blackstormdrago n
    Im having trouble with my classList variable. It's a dynamic array of strings used to store the names of the classes(my program ask for students name, number of classes, then a list of the classes).

    Code:
    typedef char* CharPtr;
    
    void Student::input()
    {
    	CharPtr classList;
    	classList = new char[];
    	int numberClasses;
    	string name;
    
    	cout<<"Enter students name ";
    	cin>>name;
    	setName(name);
    
    	cout<<"Enter the number of classes student is taking ";
    	cin>>numberClasses;
    	setNumClasses(numberClasses);
    
    	for(int index = 0; index <  numberClasses; index++)
    	{
    		cout<<"Enter name of class ";
    		cin>>classList[index];
    	}
    	setClassList(classList);
    
    }
    Everythinng works till I type in a class. If I type one letter, such as "a", everything is fine. Yet, if I type say "Math" then hit enter. It will print "Enter name of class" and then "press any key to continue". It wont let me type another class. I figured my problem is in the input() finction. I mean I think the problem is that each letter takes up an index space, but I'm just now learning about strings and dynamic arrays so I dont know what a good solution could be.
    Thanks.
    When you assigned classList = new char[], it should have given an error (my reasoning is you didn’t specify how many elements in the array, but I guess it figures you mean one element by default). Because it created a one element char array, you could do a single cin to it, which is the first letter.

    I am guessing that you want the user to enter a number of classes, so classList should be a vector of strings.

    Hope that helps,


    Adrian

    Comment

    • blackstormdragon
      New Member
      • Feb 2007
      • 32

      #3
      I'd use a vector, but this is a class assignment(whic h I should have mentioned.)
      The assignment stated that "classList – A dynamic array of strings used to store the names of the classes that the student is enrolled in"

      Now I did change classList = new char[], to classList = new char[numberClasses]; but that didnt help.

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by blackstormdrago n
        I'd use a vector, but this is a class assignment(whic h I should have mentioned.)
        The assignment stated that "classList – A dynamic array of strings used to store the names of the classes that the student is enrolled in"

        Now I did change classList = new char[], to classList = new char[numberClasses]; but that didnt help.
        Tell me how you would do it with a vector. We'll move on from there.


        Adrian

        Comment

        • blackstormdragon
          New Member
          • Feb 2007
          • 32

          #5
          It would look something like this wouldnt it.

          Code:
          vector<char> classList;
          
          for(int index = 0; index <  numberClasses; index++)
          {
          	cout<<"Enter name of class ";
          	cin>>className;
                          classList.push_back(className);
          
          }

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            Originally posted by blackstormdrago n
            It would look something like this wouldnt it.

            Code:
            vector<char> classList;
            
            for(int index = 0; index <  numberClasses; index++)
            {
            	cout<<"Enter name of class ";
            	cin>>className;
                            classList.push_back(className);
            
            }
            No, not quite. You told me:
            Originally posted by blackstormdrago n
            The assignment stated that "classList – A dynamic array of strings used to store the names of the classes that the student is enrolled in"
            Think about it and try again. Your almost have it. Once you see it, you'll be kicking yourself (like I have done on several assignments ;))


            Adrian

            Comment

            • blackstormdragon
              New Member
              • Feb 2007
              • 32

              #7
              Should I be using string instead of char??? If so, then I must of done something wrong before.

              Comment

              • AdrianH
                Recognized Expert Top Contributor
                • Feb 2007
                • 1251

                #8
                Originally posted by blackstormdrago n
                Should I be using string instead of char???
                Yes, use string instead of char.
                Originally posted by blackstormdrago n
                If so, then I must of done something wrong before.
                Why do you think you did something wrong before?

                You could use an array of an array of chars, but if you do so, you need to handle the buffer overflow problem.

                I.e. If your array of chars is say 20 chars long and someone types in a word that is 20 or more characters long, your programme may do something unexpected as it will go past the end of the buffer running in to some other portion of memory.

                Of course, your prof may not require this, but you should ask if that is the case and setup your code accordingly.


                Adrian

                Comment

                • blackstormdragon
                  New Member
                  • Feb 2007
                  • 32

                  #9
                  Originally posted by AdrianH

                  Why do you think you did something wrong before?
                  When I tried string before this wierd screen popped up. Now it works though, so I presumed I typed something in wrong before.
                  Thank you so much, everything works now. I really appreciate your help.

                  Comment

                  • AdrianH
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1251

                    #10
                    Originally posted by blackstormdrago n
                    When I tried string before this wierd screen popped up. Now it works though, so I presumed I typed something in wrong before.
                    Weird screen? Do you by chance remember what it said?

                    Originally posted by blackstormdrago n
                    Thank you so much, everything works now. I really appreciate your help.
                    No prob, glad to help.


                    Adrian

                    Comment

                    • blackstormdragon
                      New Member
                      • Feb 2007
                      • 32

                      #11
                      Something about a new instance of visual studio or something. It keep popping up and doing a weird debugging thing. (Hope this makes sense.)

                      Comment

                      • AdrianH
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1251

                        #12
                        Originally posted by blackstormdrago n
                        Something about a new instance of visual studio or something. It keep popping up and doing a weird debugging thing. (Hope this makes sense.)
                        No, not enough info. Well, I guess it doesn't matter right now since it works, and right now that is what counts. :)


                        Adrian

                        Comment

                        • blackstormdragon
                          New Member
                          • Feb 2007
                          • 32

                          #13
                          Originally posted by AdrianH
                          No, not enough info. Well, I guess it doesn't matter right now since it works, and right now that is what counts. :)


                          Adrian
                          Yeah, your right it works now and thats all that matters.

                          Comment

                          Working...