Hi!

I have this simple movie-list that I wont work, I get compile errors on this simple program. Could someone please help me to get this to work, should be very simple for you guys =)

Thanks in advance!

Here's the code:

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

using namespace std;  

void sort(char movies[][80])

{ 
	int move, i; 
	char temp[80]; 
	const int size = 10; 

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

			{

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


				strcpy(temp,movies[i])

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

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

int main() 

{ 

	int i;
	int number;
	char list[10][80];
	char line[80];

		ifstream ifil ("movies.txt"); 

		ifile.getline(line, sizeof(line)); 
	{     

		
		strcpy(line,list[number])

	number++;

	}


	while(! ifile.eof()) 

	{
     		ifile.getline(list[i],80);
     		i++;


	} 
	
	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)/sizeof(list[0]); i++)  
		{
			cout<<i<<" "<<list[i]<<endl;

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

		} 

		ofile.close(); 

return 0;  

}

Recommended Answers

All 14 Replies

Missing some semicolons after lines 24 and 50. You left off the e in infile on 44 and 70. Not sure why you didn't strcpy on 26 and 27. Most vital is intializing i to 0 on line 39 (since your first trip through the loop it doesn't have any value). You get some garbage when you sort (if there are less than 10 movies, that is), so either get input from the user to get that dimension or use a vector of strings.

Regardless of how "simple" it is for us, you're missing a chance to learn what the compiler is really telling you (in the case of the semicolons it was pretty plain as day). Just food for thought.

Hi and thanks for your reply and help, really appreciate it aswell as the quick answer!

May you try if the program works now, I dont have a compiler installed and my classmate must recieve this before tomorrow for school.

I was also wondering how you strcpy on line 26 and 27?

Thanks in advance!

Here's what I changed to:

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

using namespace std;  

void sort(char movies[][80])

{ 
	int move, i; 
	char temp[80]; 
	const int size = 10; 

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

			{

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


				strcpy(temp,movies[i]);

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

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

int main() 

{ 

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

		ifstream ifile ("movies.txt"); 

		ifile.getline(line, sizeof(line)); 
	{     

		
		strcpy(line,list[number]);

	number++;

	}


	while(! ifile.eof()) 

	{
     		ifile.getline(list[i],80);
     		i++;


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


	sort(list); 
	ifile.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)/sizeof(list[0]); i++)  
		{
			cout<<i<<" "<<list[i]<<endl;

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

		} 

		ofile.close(); 

return 0;  

}

Why do you need strcpy(temp,movies[i]); and not just temp = movies[i] ? The same reasoning applies for 26-27.

Excluding 26-27 there are no other errors in compilation.

I dont have a compiler installed and my classmate must recieve this before tomorrow for school.

If you don't have a complier directly in your possession you must have shell access to a system that does somewhere. If not find one of the many free compiler + IDE options out there...

Thanks again for your help and quick answer!

I will change the lines at 26 and 27 and see what my classmate think of this, hope it works.

Really, thank you!

You said:

I get compile errors on this simple program.

and then you said:

I dont have a compiler installed

????

Yes, my classmate i actually the one who gets the compile errors since I cant get them because I dont have a compiler (for now).

Do you think this program will work though?

Thanks in advance!

You get some garbage when you sort (if there are less than 10 movies, that is), so either get input from the user to get that dimension or use a vector of strings.

That's still one of the major caveats, I think.

Hi again!

I was told to not bring in vectors in the program and I therefore must stay with chars as I have now :/ Can you think of a better way to structure it.

Thanks in advance!

Hi again!

I was told to not bring in vectors in the program and I therefore must stay with chars as I have now :/ Can you think of a better way to structure it.

Thanks in advance!

Prompt the user for the number of movies in the file. Then use that in the declaration of the arrays.

I'm not sure if I know what you mean, if I tell the user to specify the number of movies in the file how do I do then when declarating, Im really confused here :/

Thanks in advance

The easiest way

const int nummovies = 10;
char list[nummovies][80];

That way if you need that dimension other places you can just change it at one.
(otherwise you have to dynamically declare the 2D array and that's probably too messy )

First of all I wanna thank you again, all of you!

Im not sure where to put this code though, would you care to explain it for me in simple terms =)

Thanks in advance!

In place of

int main() 

{ 

	int i=0;
	int number;
	char list[10][80];
        etc.

put

int main() 

{ 
	int i=0;
	int number;
	const int nummovies = 10;
        char list[nummovies][80];

In declaring the 2D array dynamically (using new and pointers) it changes a healthy portion of your program around. That's why I recommended using the constant..
Since you already have a similar constant in your sort method you could move it out of main into global scope and rename size in the sort method to be nummovies (or vice versa) -- just beware of globals because they can cause problems.

Thank you!

Realy appreciate it, gonna take a look at it right now!

Thanks again!

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.