Hi!

For my last assignment I was told do create a program which can read-, sort- sand save a textfile (.txt).

For example, I have a textfile containing 5 movies, this file is going to be read by the program and then sorted (in alphabetical order) and then saved to a new file.

My textfile (movies.txt) is the following:

Jurassic Park
Superman Returns
Batman Begins
The Simpsons
The Shawshank Redemption

Once sorted it must look like this (sorted_movies.txt):

Batman Begins
Jurassic Park
Superman Returns
The Shawshank Redemption
The Simpsons

Here is what I came up with:

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;


void sort(char *movies[]) 
{

int pass, i; 
char *temp; 
const int size = 5;

for (pass=0; pass < size - 1; pass++) 
{ 
for (i=0; i<size - 1; i++) // 
{
if (strcmp(movies[i], movies[i+1]) > 0)
{ 
temp = movies[i];
movies[i] = movies[i+1];
movies[i+1] = temp;

}
}
}
}

//---------------------------------------------------------------------------------------------

int main() 
{
ifstream ifil ("movies.txt");

int Quantity = 0;
string Word;



while ( ifil >> Word ) {

Quantity++;
}


cout << "Read " << Quantity << " words." << endl;

int i;
char *list[] = {"e", "c", "d", "b", "a"}; 
sort(list);

ifil.close();

ofstream ofile;
ofile.open("sorted_movies.txt");


cout<<"Sorted in order \n"<<endl; 
ofile<<"Sorted in order\n"<<endl;

for (i = 0; i < sizeof(*list)+1; i++) 
{
cout<<i<<" "<<list[i]<<endl;
ofile<<i<<" "<<list[i]<<endl; 
}

ofile.close();

return 0;


}

I cant get it to work, and I must read lines not words :/

Take a look at the code colouring in the above snippet. i.e. missing " in line 46.

oops, fixed that now, but that isnt what's wrong, nothing works :/

You are trying to do it the hard way

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    vector<string> movieList;
    ifstream in("myfile.txt");
    if( in.is_open() )
    {
        string line;
        while( getline(in, line) )
            movieList.push_back(line);
        sort(movieList.begin(), movieList.end());
        vector<string>::iterator it = movieList.begin();
        for(; it != movieList.end(); it++)
            cout << *it << "\n";

    }
}

It's probably an assignment, Dragon.
What problems are you seeing? It's hard for us to divine these answers without any real info.

Hi again!

Yes ofcourse it's an assignment, I wrote that directly.

Thanks for the code, but I forgot to say it must be about "arrays and functions" thats way it did it the hard way :/

How will I do then?

1) use getline(), not >> to read each line of the time.

2) you have to put the line that was read into an array of some kind. Line 42 just mearly counts the number of words read and toss out the strings.

move lines 48 and 49 up to just after line 32. Then change the array like this: string list[10]; When the file is read (line 42) add the lines to the array list.

Do you mean something like this:

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;  

void sort(char *movies[]) 

{ 
int pass, i; 
char *temp; 
const int size = 5; 

for (pass=0; pass < size - 1; pass++) 
{ 
for (i=0; i<size - 1; i++)  

{

if (strcmp(movies[i], movies[i+1]) > 0)
{
 temp = movies[i];
movies[i] = movies[i+1];
movies[i+1] = temp; 
}
}
}
}

 //--------------------------------------------------------------------------------------------- 

int main() 

{ int i;
string list[10]; 
int Quantity = 0;

string line;    

ifstream ifil ("movies.txt");   

while ( getline(ifil, line) ) 
{     

list[Quantity] = line;    

Quantity++;

}
cout << "Read " << Quantity << " words." << endl; 

sort(list); 
ifil.close(); 
ofstream ofile;

ofile.open("sorted_movies.txt");  

cout<<"Sorted in order \n"<<endl; 

ofile<<"Sorted in order\n"<<endl; 

for (i = 0; i < sizeof(*list)+1; i++) 
{
cout<<i<<" "<<list[i]<<endl;

ofile<<i<<" "<<list[i]<<endl; 

} 

ofile.close(); 

return 0;  

}

No

int main() 
{

int i;
string list[10]; 
int Quantity = 0;
string line;   

ifstream ifil ("movies.txt"); 


while ( getline(ifil, line) ) 
{ 
    list[Quantity] = line;
    Quantity++;
}

Next you will have to change that sort function to take an array of strings instead of an array of char pointers

void sort( string movies[], int Quantity)
{

}

Hi again!

I have now changed what you wrote in the code, hope this works =)

I also printed it out to revise it thoroughly, hope that helps me with my future coding :P

Thank you very much!

//Adam

Hi again you all!

Can anyone help me to do this with char and not string, my teacher Veronica asked me not to simplify it with string :/

Here is what I have:

#include <iostream>	
#include <cstdlib>
#include <fstream>	

using namespace std;  

void sort(char *movies[]) 

{ 
	int move, i; 
	char *temp; 
	const int size = 5; 

		for (move=0; move < size - 1; move++) 
			{ 
				for (i=0; i<size - 1; i++)  

			{

		if (strcmp(movies[i], movies[i+1]) > 0)
			{

				

				temp = movies[i];
				movies[i] = movies[i+1];
				movies[i+1] = temp; 
			}
			}
		}
		}

//--------------------------------------------------------------------------------------------- 

int main() 

{ 

	int i;
	char list[10]; 
	int line = 0;

	char line;

		ifstream ifil ("movies.txt");

		while ( getline(ifil, line) ) 
	{     

		list[number] = line;    

	number++;

	}
	
	cout << "Read " << number << " lines." << endl; 

	sort(list); 
	ifil.close(); 
	ofstream ofile;

	ofile.open("sorted_movies.txt");

	cout<<"Sorted in order \n"<<endl; 

	ofile<<"Sorted in order\n"<<endl; 

		for (i = 0; i < sizeof(*list)+1; i++) 
		{
			cout<<i<<" "<<list[i]<<endl;

			ofile<<i<<" "<<list[i]<<endl; 

		} 

		ofile.close(); 

return 0;  

}

Thanks in advance!

//Adam

line 41: variable list needs to be an array of pointers. What you have coded is a simple character array. Declare it like this: char *list[10]; line 42: variable line needs to be a character array, not an integer. Declare it like this: char line[80]; .


line 48: There are two versions of getline() -- one for std::string and another for character arrays. What you have coded is the one for std::string. Here's how to code it for character arrays: ifile.getline(line, sizeof(line)); line 51: You can't insert a string into the array like that -- that is the std::string way of doing it. In C style strings you have to do it the hard way by first allocating memory for the string and then calling strcpy() to copy it into the array.

line 51: Variable number is undeclared.

line 69: Your use of sizeof will produce the wrong result. There are 10 strings in the array, but sizeof(*list) will return a value of 4, which is the size of a pointer. To get the number of elements in the array you need sizeof(list)/sizeof(list[0]) which is the size of the array divided by the size of one element of the array.

And why are you adding +1 to that sizeof ? That will make the loop iterate once too many times.

line 13: You need 10 there, not 5.

lines 15, 16 and 17: There are quite a few sort algorithms, but I don't think that is one of them. That will do a lot of unnecessary comparisons, such as comparing a line to itself. Here is a slightly better way to do it, This is sufficient for small arrays like yours, but for larger arrays you should investigate other sort algorithms.

int i,j;
for(i = 0; i < size-1; i++)
{
    for(j = i+1; j < size; j++)
    {
        if( strcmp( list[i], list[j] ) > 0)
        {
               // swap the pointers
        }
    }
}
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.