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:

#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 fine.
                    }
                }
        }
    }
    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 text file.
    for (int i=0; i<10000; i++)
    {
        o << 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!
            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!!

Dani AI

Generated

Good catches from and @Ancient Dragon: strcmp expects C strings, not std::string. For C++ strings, compare with the relational operators (<, >, etc.) or string::compare. Two more issues to fix: (1) your inner loop goes out of bounds when it checks arr[index+1]; stop one element earlier, and (2) length = 10,000; uses the comma operator and actually assigns 0. Write 10000 (no comma) or, better, compute the count you actually read.

Also simplify the file load. Avoid while (!eof()) and nested loops; instead, read line-by-line and track how many you got. Here is a compact pattern that both loads and bubble-sorts safely:

#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;

static void bubble_sort(vector<string>& a) {
    for (size_t i = 0; i + 1 < a.size(); ++i) {
        bool swapped = false;
        for (size_t j = 0; j + 1 < a.size() - i; ++j) {
            if (a[j] > a[j + 1]) { swap(a[j], a[j + 1]); swapped = true; }
        }
        if (!swapped) break; // early exit if already sorted
    }
}

int main() {
    ifstream in("Movies.txt");
    vector<string> lines;
    string line;
    while (lines.size() < 10000 && getline(in, line)) lines.push_back(line);

    bubble_sort(lines);

    ofstream out("Bubble.txt");
    for (const auto& s : lines) out << s << '\n';
}

Notes:

  • If you must keep a raw array, pass the actual n you read and loop with for (int j = 0; j < n - 1 - i; ++j).
  • Bubble sort is O(n^2); for 10k titles that is tens of millions of comparisons. If the assignment allows it, replace the sort with std::sort(lines.begin(), lines.end()); which is much faster and still compares strings lexicographically.

Recommended Answers

All 2 Replies

Since you're already using C++ strings, consider using string::compare instead of strcmp.

strcmp() wants two char* not two std::string

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.