bubble sorting of strings

This is a discussion on bubble sorting of strings within the C++ Programming forums, part of the General Programming Boards category; Hey Guys! I have to read data from a text file, load it into an array and then bubble sort ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    27

    bubble sorting of strings

    Hey Guys! I have to read data from a text file, load it into an array and then bubble sort it! Here is my code:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    void Bubble_Sort(string arr[], int length);
    int main()
    {
        int length;
        length=10,000;
        ifstream MyFile;
        ofstream out;
        string line;
        string arr[10000];
        
        //out.open("Write.txt");
        MyFile.open("Movies.txt");
        if (MyFile.is_open())
        {
            while (!MyFile.eof())
            {
                    //out << line << endl;
                    for (int i=0; i<10000; i++)
                    {
                        while (getline (MyFile,line,'\n'))
                        {
                          arr[i] = line;    // works perfectly alright!
                         
                        }
                    }
            }
        }
        ofstream o;
        o.open("Bubble.txt");
        if (o.is_open())
        {
            cout << " file is opened " << endl;
        }
        else 
        {
            cout << " file not opened " << endl;
        }
        Bubble_Sort(arr,length);
        // writing sorted names to a file
        for (int i=0; i<10000; i++)
        {
            out << arr[i] << endl;
        }
    
        system("pause");
        return 0;
    }
    
    void Bubble_Sort(string arr[], int length)
    {
        string temp;
        int iteration;
        int index;
        for (iteration=0; iteration<length; iteration++)
        {
            for (index=0; index<length-iteration; index++)
              {
               if (strcmp(arr[index], arr[index+1])){  // error! underlines arr
                temp = arr[index];
                arr[index] = arr[index+1];
                arr[index+1] = temp;
                  }
            }
        }
    }
    Basically what i am trying to do is that sort the names of 10,000 movies and then write those names to a text file. It gives an error in the nested loop by underlining arr and tells that no suitable conversion function from std::string to const char* exists. Please help me out with this. Thanks in advance!!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    You don't need the C function strcmp() to compare C++ strings. The C function strcmp() works with C-strings (character strings) not std::string.

    Jim

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Why do you use strcmp for a std::string? strcmp receives char*, not strings! You have to cast if you must use strcmp, but I do not think you are obliged to do something like that.

    Note also, that when the strings are equal, strcmp returns 0. strcmp - ref for more.

    However std::string has overloaded operators, so for equality, you could something like this:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
     
    int main() {
     
        string s1 = "Sam";
        string s2 = "Sam";
        if(s1 == s2)
            cout << "Equal strings\n";
        else
            cout << "Unequal strings\n";
        return 0;
    }
    Code - functions and small libraries I use


    Itís 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " óHarold Abelson

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    So which function should i use ?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    The comparison operator.
    Code:
    if( string1 == string2)
    You have to cast if you must use strcmp,
    No you would need to use the std::string.c_str() method function if you wanted to use strcmp().

    Jim

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    do not use eof to control loop

    why do you have 3 nested loops to read a file? Use one.
    Code:
    length=10,000;
    will not do what you think it should do...

    Check this:

    Code:
    #include <iostream>
    int main()
    {
        int length;
        length=10,000;
        std::cout << length << std::endl;
        return 0;
    
    }


    Comma Operator: ,
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble Sorting Problem
    By jappy512 in forum C Programming
    Replies: 3
    Last Post: 12-02-2009, 05:54 PM
  2. Bubble Sorting
    By yukapuka in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2008, 09:44 PM
  3. bubble sorting a 2-d array
    By stodd04 in forum C Programming
    Replies: 5
    Last Post: 03-17-2005, 12:40 PM
  4. bubble sorting multiple strings
    By jibbles in forum C Programming
    Replies: 10
    Last Post: 10-11-2003, 11:48 PM
  5. Bubble Sorting HELP WANTED !
    By Shahram_z in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2003, 07:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21