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

    Plz help to Sort String Values in Ascending Order by declaring arrays

    Hello,

    I am new to C++ and have so interest in this programing....im trying to Sort String Values in Ascending Order by declaring arrays...

    Actually i want to get input form the user of five words (Cat, Bat, Arm, Dog, Man) and then want to sort them in ascending order like (Arm, Bat, Cat, Dog, Man) and then want to show it on output.

    i tried code but i am getting error can you tell me what is the problem in array string and can you please write and justify the complete this code so that i get output with no errors...thanks

    here is code:

    #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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    What is the error? Please post your code within [code][/code] bbcode tags.
    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

  3. #3
    Join Date
    Apr 2009
    Posts
    16

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    like these errors i get when i compile n run it.

    invalid conversion from `char' to `char*'
    initializing argument 1 of `std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'

    can you guyz plz recode this program if it is not right coding....i just want to Sort String Values in Ascending Order by declaring arrays..

    i need your coding to see what is the right coding for this program..

    Code:
     #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;
    }

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    Quote Originally Posted by itguy310
    invalid conversion from `char' to `char*'
    Right. Why do you have both s and array? Why not use an array of std::string objects?

    Quote Originally Posted by itguy310
    i need your coding to see what is the right coding for this program..
    Would it really help you?
    Code:
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    
    int main()
    {
        using namespace std;
        const size_t num_words = 5;
        string words[num_words];
        cout << "Enter " << num_words << " words followed each by <enter> " << endl;
        for (size_t i = 0; i < num_words; ++i)
        {
            getline(cin, words[i]);
        }
    
        sort(words, words + num_words);
    
        cout << "\nIn sorted order:\n";
        copy(words, words + num_words, ostream_iterator<string>(cout, "\n"));
    }
    Notice that my example is well indented.
    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

  5. #5
    Join Date
    Apr 2009
    Posts
    16

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    Thank you so much laserlight,

    yes your code is really well indented, i appricaite your help.........yes it is sorting out the unsorted string entered by the user..but i want to do this by declaring character type string array and using for loop. How can i do that ? as i was trying to, see my above post by declaring char type array but could not do it well.

    and also want to print the the string message sequencly on the sacreen instead of "Enter 5 words followed each by <enter>" to enter words like this:

    Input string 1 : Cat
    Input string 2 : Bat
    Input string 3 : Arm
    Input string 4 : Dog
    Input string 5 : Man

    and then sort it out..............so how we can code it ?
    Last edited by itguy310; April 11th, 2009 at 03:32 AM.

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

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    Quote Originally Posted by itguy310 View Post
    Thank you so much laserlight,

    yes your code is really well indented, i appricaite your help.........yes it is sorting out the unsorted string entered by the user..
    This is what you asked for...
    i need your coding to see what is the right coding for this program..
    ...and that is exactly what laserlight gave you -- a C++ solution.

    Anything else is using old 'C' techniques that are not necessary in a C++ program. If you want to code using 'C', then that is what you should have asked for. No C++ programmer would have declared a "string array" as you did, where the goal is to collect names in an array. At the very least:
    Code:
    #include <string>
    #include <iostream>
    //...
    using namespace std;
    
    int main()
    {
       string allMyNames[5]; // an array of 5 string;
       for (int i = 0; i < 5; ++i )
       {
           cout << "Enter name " << i+1 << ": ";
           std::getline(cin, allMyNames[i]);   
       }
    }
    That should have been the very first or near the very first attempt any C++ programmer would have tried.

    What you're asking for is a 'C' solution, not a C++ solution.
    but i want to do this by declaring character type string array and using for loop. How can i do that ? as i was trying to, see my above post by declaring char type array but could not do it well.
    Let's take a look at the code you did write:

    First, in your post, you do nothing with std::string, but you're including the <string> header.

    Second,
    Code:
    char s[5];
    //...
    for(int i=0; i<5; i++)
    {
       cin.getline(s[i],20);
    }
    The s[ i ] is a single character. The getline() requires an array of characters, not a single character.

    I think you should take a step back and write a very simple program to input 5 'C' strings.
    Code:
    #include <iostream>
    using namespace std;
    
    char array[5][80];
    
    int main()
    {
        for (int i = 0; i < 5; ++i )
        {
            cout << "Enter name " << i+1 << ": ";
            cin.getline(array[i], 80);   
        }
    }
    and then sort it out..............so how we can code it ?
    Sorting names requires a sort algorithm. Which one are you going to use?

    Start with reading the names correctly as above. Then when you have a specific question (not the open-ended "how we can code it?"), then ask that question.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Apr 2009
    Posts
    16

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    thanks Paul for your detail.....sorry if i am asking it by writing wrong wordings. May be the code i did above is wrong because as i told you i am very new to c++, that's why may be i could not identify it properly either it is of C or C++.

    Leave the code that i wrote in above post. Here i repeat my question again and i hope you will be getting me what i am trying to say. (Sorry if im asking it not in write way).

    Actually i want to do this in C++ not in C. The code that wrote "lasterlight" that is very near to my question and he did well. here is his code:
    Code:
     #include <string>
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    
    int main()
    {
        using namespace std;
        const size_t num_words = 5;
        string words[num_words];
        cout << "Enter " << num_words << " words followed each by <enter> " << endl;
        for (size_t i = 0; i < num_words; ++i)
        {
            getline(cin, words[i]);
        }
    
        sort(words, words + num_words);
    
        cout << "\nIn sorted order:\n";
        copy(words, words + num_words, ostream_iterator<string>(cout, "\n"));
    }
    Actually in this code, the message on screen to get input from the user is Enter 5 words followed each by enter but i want to replace it by the following text that will displayed on screen:

    enter your string

    thats it.
    And the code that you wrote that is also very near to my question:
    Code:
    #include <string>
    #include <iostream>
    //...
    using namespace std;
    
    int main()
    {
       string allMyNames[5]; // an array of 5 string;
       for (int i = 0; i < 5; ++i )
       {
           cout << "Enter name " << i+1 << ": ";
           std::getline(cin, allMyNames[i]);   
       }
    }
    but in your code you just getting input from the user with exact output text that i want to use like "Enter name 1" or "Input string 1" , but your code is not sorting the words entered by user, Can you please write the code to sort them ?

    I hope you getting me what i am trying to ask, would be waiting for your kind response.

    many thanks.
    Last edited by itguy310; April 11th, 2009 at 01:00 PM.

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

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    Quote Originally Posted by itguy310
    Actually in this code, the message on screen to get input from the user is "Enter 5 words followed each by <enter>" but i want to replace it by the following text that will displayed on screen:
    Quote Originally Posted by itguy310
    thats it. And the code that you wrote that is also very near to my question but in your code you just getting input from the user with exact output text that i want to use like "Enter name 1" or "Input string 1" , but your code is not sorting the words entered by user, Can you please write the code to sort them ?
    Okay, we have given you one example that does almost what you want and another example that does almost what you want, and the union of these two examples will give you what you want. Give it a shot, you have nothing to lose.

    Quote Originally Posted by itguy310
    I hope you getting me what i am trying to ask, would be waiting for your kind response.
    Yes, I hope that I got what you are trying to ask, and would be waiting for your attempt to solve your problem using our examples as a guide.
    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
    16

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    thanks for reply laserlight. Please see my above discription in detail...waiting.thanks

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    Look, we have to stop spoonfeeding you eventually. You made an attempt before starting this thread, and that is good. Why not make an attempt to understand our examples and derive your solution from them?
    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

  11. #11
    Join Date
    Apr 2009
    Posts
    16

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    sorry if i am bothering you, i am trying to do that but cant until i get the proper guide from you professional guys it really helping me a lot.

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

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    Compare a snippet of my example:
    Code:
    cout << "Enter " << num_words << " words followed each by <enter> " << endl;
    for (size_t i = 0; i < num_words; ++i)
    {
        getline(cin, words[i]);
    }
    
    sort(words, words + num_words);
    with a snippet of Paul McKenzie's example:
    Code:
    for (int i = 0; i < 5; ++i )
    {
       cout << "Enter name " << i+1 << ": ";
       std::getline(cin, allMyNames[i]);   
    }
    Can't you see the differences and the significance behind the differences?
    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
    Oct 2008
    Posts
    68

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    You can copy the words into a string array, and then use strcpy to copy the contents of the strings into char arrays, and then compare the values of the first character in the array (the beginning letter), to see where the order of the words should be. When you know the order, then you can easily output the string array in the correct order. So if StringArray[3] is first, cout it onto the screen first.

  14. #14
    Join Date
    Apr 2009
    Posts
    16

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    Yes i have done it and my problem is solved. Thank you so much laserlight and Paul.

    I combine both code of yours and now i am getting the exact output that i want...thanks again. You guys really doing very good job, and i got how string arrays works.

    Cheers....

  15. #15
    Join Date
    Apr 2009
    Posts
    16

    Re: Plz help to Sort String Values in Ascending Order by declaring arrays

    thnks laserlight, Its done..!

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