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!!