How to ctreate an array of objects that contain character strings
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: How to ctreate an array of objects that contain character strings

  1. #1
    Join Date
    Apr 2009
    Posts
    6

    Question How to ctreate an array of objects that contain character strings

    I've just strated learning object oriented c++ and I'm having a lot of confusion with it. Could anyone tell me how to create an array of objects that contain character strings. I want to input the names of 5 items. How do I create an array that contains these 5 words, and then how do I retrieve those words from the array?

  2. #2
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: How to ctreate an array of objects that contain character strings

    Strings: string items[5];
    General: myclass items[5];

    To retrieve the item "n" from the string solution, just items[n].
    To retrieve the item "n" from the "myclass" solution, overload the operator [ ], and then just items[n];

    Edit:
    Maybe the problem is you don't know how to create the class. Example:
    Code:
    class MyClass{
      public:
          string& operator[](int n){ return item[n]; }
      private:
          string item[5];
    }
    Last edited by Tronfi; April 5th, 2009 at 06:46 AM.

  3. #3
    Join Date
    Mar 2008
    Posts
    30

    Re: How to ctreate an array of objects that contain character strings

    Code:
    char array[5][80];
    /*Declares an array that has 5 elements.
    Each of the element has 80 elements.
    The first dimension represents the 5 words and the second dimension represents the maximum length of each words.*/
    
    for(int i=0; i<5; i++)
    {
         std::cout << array[i] << "\t";
    }//Prints the 5 words.
    You can also use the string class. To use it, include string to your cpp file
    Last edited by richard_tominez; April 5th, 2009 at 06:52 AM.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,434

    Re: How to ctreate an array of objects that contain character strings

    Quote Originally Posted by Tronfi View Post
    Edit:
    Maybe the problem is you don't know how to create the class.
    Why create a class? The array is all that's needed.

    Regards,

    Paul McKenzie

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: How to ctreate an array of objects that contain character strings

    The solution that most closely matches the OP's request is simply
    Code:
    std::string myarray[5];
    since a std::string is an "object that contains a character array".

    The example given by richard_tominez is simply an array of arrays, not an array of objects; and it's less safe to work with (although perhaps marginally faster in some cases...not enough to worry about usually).
    Last edited by Lindley; April 5th, 2009 at 02:55 PM.

  6. #6
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: How to ctreate an array of objects that contain character strings

    Quote Originally Posted by Paul McKenzie View Post
    Why create a class? The array is all that's needed.

    Regards,

    Paul McKenzie
    I know it's not neccesary to create a class, but I supoussed (maybe wrongly) that it was what he wanted. I thought he knew how to use string (I said how to make an array of strings in the beginning of my reply), but he said he was new in "object oriented c++", so I give him a example with a home-made class.

  7. #7
    Join Date
    May 2007
    Posts
    800

    Re: How to ctreate an array of objects that contain character strings

    Fully dynamic
    Code:
    std::vector<std::string> myarray;

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,317

    Re: How to ctreate an array of objects that contain character strings

    Quote Originally Posted by STLDude
    Fully dynamic
    Except that leonida explicitly wants an array of 5 string objects, so if we are to use a container instead of a C-style array, it should be:
    Code:
    std::tr1::array<std::string, 5> names;
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Apr 2009
    Posts
    6

    Re: How to ctreate an array of objects that contain character strings

    Thanks for your replies. Tronfi, you got the point, I'm looking strictly for a solution with class objects. I know how to create arrays but I don't know how to do it within a class.

    Please can someone show me a code where you let user enter 5 words, which are stored in array and then you access those words in array through iteration loop and display. It has to use class. Is it possible to do such thing?

    I appreciate your help.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,434

    Re: How to ctreate an array of objects that contain character strings

    Quote Originally Posted by leonida View Post
    Thanks for your replies. Tronfi, you got the point, I'm looking strictly for a solution with class objects.
    I don't really understand what you really want. Do you want an array of strings? If you do, what was shown to you by others is what you are asking for.

    If you want an array of an object, and each object contains a string, that is a different story.

    So which one do you want? If it is an object with a string, then here it is:
    Code:
    class SomeObject
    {
       private:
            std::string m_myString;
       public:
           // other members
    };
    
    int main()
    {
       SomeObject arrayOfObject[5];
    }
    Please can someone show me a code where you let user enter 5 words, which are stored in array and then you access those words in array through iteration loop and display.
    This can easily be done with an array of std::string
    It has to use class.
    Unless you mean use the std::string class, this doesn't make sense. You need to explain exactly what your requirements are.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Apr 2009
    Posts
    6

    Re: How to ctreate an array of objects that contain character strings

    Paul, when I say class, I don't mean specific one, just as many classes as necessary, but my confusion is how to create an array of objects, a data member should be a pointer to null-terminated string. I don't understand how to input string objects into an array., I do know how to declare an object whose data member is a pointer to string, but I really don't know how to implement that into arrays and then retrieve data from an array.

    Could you show me just a minimum piece of code that declares an array of objects, lets say, how would you imput 3 words: for example "cats", "dogs", "ducks" and then display the word "cats" in cout statement. I just want to see how you index those words.

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,317

    Re: How to ctreate an array of objects that contain character strings

    Quote Originally Posted by leonida
    but my confusion is how to create an array of objects, a data member should be a pointer to null-terminated string
    As demonstrated, use a std::vector<std::string>. You would access it just as you would a "normal" array.

    If due to assignment requirements you are not allowed to do so, then...

    Quote Originally Posted by leonida
    I do know how to declare an object whose data member is a pointer to string, but I really don't know how to implement that into arrays and then retrieve data from an array.
    Let's see your current code. Chances are, you should be creating your own string class.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Apr 2009
    Posts
    6

    Re: How to ctreate an array of objects that contain character strings

    I figured out my confusion is not coming from classes, I'm sorry if my previous posts were confusing, I was confused myself. I realized I don't know how to create the right input and output, can you tell me what is wrong in my code? This is something simple that I got stuck with.

    #include <iostream>
    using namespace std;
    #include <string>

    int main(){

    char s[5];

    cout<<"Enter 5 words followed each by <enter>"<<endl;

    for(int i=0; i<5; i++)
    {
    cin.getline(s[i],20);
    }

    for(int j=0; j<5; j++)
    {

    cout << s[j]<<endl;
    }


    return 0;
    }


    error: cannot convert parameter 1 from 'char' to 'char *'

  14. #14
    Join Date
    Apr 2009
    Posts
    6

    Smile Re: How to ctreate an array of objects that contain character strings

    Quote Originally Posted by richard_tominez View Post
    Code:
    char array[5][80];
    /*Declares an array that has 5 elements.
    Each of the element has 80 elements.
    The first dimension represents the 5 words and the second dimension represents the maximum length of each words.*/
    
    for(int i=0; i<5; i++)
    {
         std::cout << array[i] << "\t";
    }//Prints the 5 words.
    You can also use the string class. To use it, include string to your cpp file
    This solved my mistake, I forgot to enter the length of words [80]. Thank you everybody for trying to help! I'm new to this forum and I'm glad there are people like you who are willing to spread the knowledge.

  15. #15
    Join Date
    Apr 2009
    Posts
    16

    Re: How to ctreate an array of objects that contain character strings

    Hello leonida,

    i got what you trying to ask and you coded very well....actually i am also new to c++ and working on it.

    You want input from the user of five words then sort them into ascending order. i tried your code but i am getting error can you tell me what was the problem in array string and can you please write and justify the complete code so that i can also understand it more and can code it by myself.



    Where is mistake in this code? because it is not working yet..

    #include <iostream>
    #include <string>

    using namespace std;
    int main(){

    char s[5];
    char array[5][80];

    cout<<"Enter 5 words followed each by <enter>"<<endl;

    for(int i=0; i<5; i++)
    {
    cin.getline(s[i],20);
    }

    for(int j=0; j<5; j++)
    {

    cout << s[j]<<endl;
    }


    return 0;
    }





    many thanks.

    Paul.
    Last edited by itguy310; April 9th, 2009 at 02:12 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center