[RESOLVED] How would I alphabetize this?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: [RESOLVED] How would I alphabetize this?

  1. #1
    Join Date
    Apr 2012
    Posts
    37

    [RESOLVED] How would I alphabetize this?

    How would I alphabetize this with the different strings? Would I just need to use a bubble sort?
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    int main()
    {
        string my_string;
    cout << "This program is a database for storing information." << endl;
    ofstream myfile;
    myfile. open ("data.txt", ios::app);
    string x;
    string y;
    string z;
    string a;
    string b;
    getline (cin, x);
    myfile << x << endl;
    getline (cin, y);
    myfile << y << endl;
    getline (cin, a);
    myfile << a << endl;
    getline (cin, b);
    myfile << b << endl;
    myfile. close ();
    system("pause");
    return 0;
    }

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: How would I alphabetize this?

    Can you explain in bit more details what exactly you want to achieve?

    Do you want to get input from the user in form of strings and then write these strings into a file, into an alphabetical order? Or am I missing something?

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,525

    Re: How would I alphabetize this?

    Quote Originally Posted by smurf12125 View Post
    How would I alphabetize this with the different strings? Would I just need to use a bubble sort?
    You are lucky you only have five strings. What if you needed a 100? How would your code look?
    I suggest you look into some std containers. That would reduce your code to just a couple of lines, and also provide standard sort algorithm.
    Last edited by VladimirF; May 16th, 2012 at 09:36 AM.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  4. #4
    Join Date
    Apr 2012
    Posts
    37

    Re: How would I alphabetize this?

    well all I needed was a small database to store some lines of text but it uses user input and saves it to a text file, its something simple but it also doesn't save over the previous input. I just need to find a way to alphabetize the file after the inputs have been added.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,422

    Re: How would I alphabetize this?

    Quote Originally Posted by smurf12125 View Post
    well all I needed was a small database to store some lines of text but it uses user input and saves it to a text file, its something simple but it also doesn't save over the previous input. I just need to find a way to alphabetize the file after the inputs have been added.
    Code:
    #include <vector>
    #include <string>
    #include <algorithm>
    
    typedef std::vector<std::string> StringArray;
    
    using namespace std;
    
    int main()
    {
        StringArray s;
        //...
        // names have been read into s
       //...
        // now sort s
        sort(s.begin(), s.end());
    }
    As Vladimir stated, that one line of code sorts the strings if they are stored in the StringArray container.

    As mentioned, you must change your code so that it stores strings in a container, not individual string variables. How are you going to sort 100 names, let alone 5 names, if all of those variables have different names?
    Code:
    string s1;
    string s2;
    string s3;
    string s4;
    string s5;
    Now, you don't know which string comes before which other string, and the variables are all named differently. I dare you to write a sorting function for these 5 separate strings, all with different variable names. I certainly wouldn't want to do it.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 16th, 2012 at 11:42 AM.

  6. #6
    Join Date
    Apr 2012
    Posts
    37

    Re: How would I alphabetize this?

    Oh I didn't quite catch that part sorry, I'll look into algorithms now since I haven't learned about them yet. Thanks for all the advice!

  7. #7
    Join Date
    Apr 2012
    Posts
    37

    Re: How would I alphabetize this?

    Alright after researching algorithms and trying to implement it into my code I have run into another problem.
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    #include <algorithm>
    #include <vector>
    typedef vector <string> StringArray; /// line 7
    using namespace std;
    int main()
    {
    cout << "This program is a database for storing information." << endl;
        ofstream myfile;
        myfile. open ("data.txt", ios::app);
    cout << "Please input what you want to store." << endl;
    string s1;
    string s2;
    string s3;
    string s4;
    string s5;
        getline (cin, s1);
    myfile << s1 << endl;
        getline (cin, s2);
    myfile << s2 << endl;
        getline (cin, s3);
    myfile << s3 << endl;
        getline (cin, s4);
    myfile << s4 << endl;
        getline (cin, s5);
    myfile << s5 << endl;
        sort (s.begin(), s.end(), greater<int>()); /// line 30
        myfile. close ();
    system("pause");
    return 0;
    }
    I keep getting the errors
    database.cpp|7|error: expected init-declarator before '<' token|

    database.cpp|7|error: expected `,' or `;' before '<' token|

    database.cpp||In function `int main()':|

    database.cpp|30|error: `s' undeclared (first use this function)|

    database.cpp|30|error: (Each undeclared identifier is reported only once for each function it appears in.)|
    What am I doing wrong so I can figure out how to fix this?

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

    Re: How would I alphabetize this?

    The types are actually called std::vector and std::string.

    Move your "using namespace std;" line above the typedef, or else use the fully-qualified names in the typedef.

  9. #9
    Join Date
    Apr 2012
    Posts
    37

    Re: How would I alphabetize this?

    alright but why won't this work?
    Code:
    sort (s.begin(), s.end(), greater<int>());
    its saying that 's' isn't declared and if I do declare it then begin and end need to be declared so would I need to declare all three of them?

  10. #10
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: How would I alphabetize this?

    In post 5, Paul wasn't giving you code to put in your current code. He was giving you an example.

    Quote Originally Posted by Paul McKenzie View Post
    As mentioned, you must change your code so that it stores strings in a container, not individual string variables.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,422

    Re: How would I alphabetize this?

    Quote Originally Posted by smurf12125 View Post
    alright but why won't this work?
    Code:
    sort (s.begin(), s.end(), greater<int>());
    its saying that 's' isn't declared and if I do declare it then begin and end need to be declared so would I need to declare all three of them?
    You are not understanding what you're programming.
    Code:
    string s1;
    string s2;
    string s3;
    string s4;
    string s5;
    Why are you still declaring 5 string variables? Again, what if you had 1000 names? Would you declare 1,000 variables? How about 10,000 names? Would you now declare 10,000 variables and write the same code over and over again 10,000 times? What if you wanted to sort all the names in the New York City phone book listing? 10,00,000 different variables and 10,00,000 copies of the same code? See how silly it will get? If that was the only way to write such programs in C++, no one would write programs in C++.

    All you need is one variable -- a variable that can hold multiple names. That variable is the StringArray that I mentioned.
    Code:
    StringArray s;  // a container that holds 5 names
    std::string inString;
    for (int i = 0; i < 5; ++i )
    {
        getline (cin, inString);
        myfile << inString << endl;
        s.push_back( inString );
    }
    This loop does exactly what your entire code did with those 5 separate input statements. If for some reason we need 1,000 names, then all I need to do is change that loop to go to 1000 instead of 5. Do you understand what's going on now?

    Then when you sort, you are sorting s, which contains all of the names.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 22nd, 2012 at 01:25 PM.

  12. #12
    Join Date
    Apr 2012
    Posts
    37

    Re: How would I alphabetize this?

    Quote Originally Posted by Paul McKenzie View Post
    10,00,000 copies of the same code? See how silly it will get? If that was the only way to write such programs in C++, no one would write programs in C++.
    Your completely right and looping it is the best way to go thanks for the advice.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center