Thread: Can I have an "Array of Strings"?

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Question Can I have an "Array of Strings"?

    Hello everyone,

    I don't think I will be able to find the correct words to explain what I want but I will just give it a try....

    I have to make a program which takes 5 names as input and sorts them in alphabetical order. I have seen the program which sorts numbers in ascending or descending order by comparing one by one and then swapping them(I think it is called quicksort). Now I want an array of strings to store the names. I tried to do something with classes but then got confused

    I am a beginner so don't use heavy concepts please

    Thank you..

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes you can.

    Can you please post your code to read one name, so that I can determine what level of multiple names functionality would be suitable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Here is what I tried to dohope it makes some sense)
    Code:
    #include<iostream>
    using namespace std;
    void sort();
    void display();
    class name
    {
          private:
                  char nm[20];
          public:
                 name()
                 {
                       cout<<"Enter the name";
                       cin>>nm;
                 }        
                 void show_name()
                 {
                      return nm;
                 }      
    }
    
    int main()
    {
        name nm1,nm2,nm3,nm4,nm5;
        sort();
        display();
        cin.get();
        return 0;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so if you are using character arrays as strings (rather than C++ std::string), then you will need to use a 2D array, something like this:
    Code:
    char name[5][20];
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Thanks! I am going to try it right now. I will come back if I need some more help....

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    string is an STL collection of characters, you should use an STL container such as a vector or list to store them. using C style arrays when you have these vectors makes use of the STL.

    Code:
    std::vector<std::string> strCol;
    strCol.push_back("SomeString);
    /...etc etc
    This dynamically resizes the vector so you don't have to when doing something suchj as sort or remove etc. Most of the vector interface is supplied in the class.

    http://www.cppreference.com/cppvector/index.html

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    indigo: I choose to NOT suggest that, because I believe that the poster is currently working on character arrays to learn those - whilst using vectors certainly is a good thing, I don't agree with suggesting a completely different system for almost the entire functionality in the code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I don't know, I just find if he's a beginner it would be easier to understand the abstracted string and vector class rather than the underlying implementation which is what he's doing. To create a string he'd have to jump through hoops to resize it, then dynamically allocate a new vector for the alpha sorting. I think it's more trouble than it's worth. He can learn about the C origins later, but I think it would hurt to try to understand that first. Mainly because it introduces the idea of pointers too early for him to make use of properly.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure.

    The other question that is relevant is if this is a school assignment or self-education. In the former case, the class direction would also be relevant.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    If the OP doesn't have to, he shouldn't. Otherwise I'd tell him to punch his teacher.

  11. #11
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    I Wrote This:

    I am trying something similar to quicksort.
    I wrote this but it is giving me some errors:
    Code:
    #include<iostream>
    #include<algorithm>
    void sort();
    void display();
    char names[5][20];
    using namespace std;
    int main()
    {
        char temp[20];
        int x=0,y=0;
        for(x=0;x<=4;x++)
        {
                cout<<endl<<"Enter the "<<x<<"th name";
                cin>>temp;   //I used this temporary string for reason I can't explain in words!
                for(y=0;temp[y]!='\0';y++)
                {
                        names[x][y]=temp[y];
                }
                names[x][y]='\0';
        }          //I have tested the code upto this point.I could make five inputs and then print them one by one.
        sort();
        display();
        cin.get();
        return 0;
    }
    
    void sort()
    {
         int x,y,var,num1,num2;
         for(x=0;x<4;x++)
         {
                    for(var=x+1;var<=4;var++)
                    {
                                             for(y=0;((names[x][y]!='\0')&&(names[var][y]!='\0'));y++)
                                             {
                                                                                                      
                                                                                                      num1=(int)names[x][y];   //This is to get the ASCII value
                                                                        
                                                                                                      num2=(int)names[var][y];//This is to get the ASCII value
                                                                                                      
                                                                                                      if(num1>num2)
                                                                                                      {
                                                                                                                 
                                                                                                                   swap(names[x],names[var]);
                                                                                                      }
                                                                                                      
                                             }
                    }
         }
    }
    
    void display()
    {
         for(int x=0;x<=4;x++)
         {
                 cout<<endl<<names[x];
         }
    }
    I know I have not made the code readable.....sorry for that
    Code:
    The error messeges are:
     C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algobase.h In function `void std::swap(_Tp&, _Tp&) [with _Tp = char[20]]': 
    43 C:\Dev-Cpp\alpha.cpp   instantiated from here 
    130 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algobase.h invalid initializer 
    131 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algobase.h ISO C++ forbids assignment of arrays 
    132 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algobase.h ISO C++ forbids assignment of arrays

    What do you think is the problem? (If you can read that bad coding)
    Thanks!

  12. #12
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    This is self-education but I don't know anything about vectors (I have studied vectors in physics--magnitude and direction, will that much do?)

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You might try std::string then. For example, two std::strings can be compared with the comparison operator, but you'd need to use strcmp for C-style strings. std::strings can be assigned, whereas arrays need to be copied each element individually.

    In addition std::string automatically avoids buffer overruns which your current code is in danger of (what if I typed more than 19 characters?)

    In std::vector vector simply means a sequence. It is basically an array, but it is smarter than a plain array in the same way a std::string is smarter than a C-style string (e.g knows its size, can resize itself automatically, automatically manages memory etc).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    also the algorithms library you included is made to handle all sorts of Generalized STL Containers such as vectors and lists.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> also the algorithms library you included is made to handle all sorts of Generalized STL Containers such as vectors and lists.

    The algorithms library is generic and works on C style arrays just as well as C++ strings, vectors, lists, etc.

    That said, I, too would suggest learning C++ strings and vectors rather than C style arrays. There is very little reason to use the C style versions in C++ programs. The string class is rather easy to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "array does not point to an object type"
    By Welshy in forum C Programming
    Replies: 2
    Last Post: 02-27-2006, 02:06 PM
  2. Essentially an "array of pointers" problem
    By Matt13 in forum C Programming
    Replies: 4
    Last Post: 03-12-2004, 03:36 AM
  3. Sorting "numerical strings"
    By dirkduck in forum C++ Programming
    Replies: 7
    Last Post: 01-18-2003, 05:13 PM