#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    65
    Rep Power
    11

    Basic Programming Problem


    hello ppl, i need help correcting my program, im having a hard time finding the problem...

    I have this input:
    6
    Seabiscuit
    240
    Identity
    87
    The Monster and the Girl
    65
    Chicago
    160
    Daredevil
    167
    Jurassic Park
    127
    (the 6 is the number of movies for the loop control)

    I need to output to the screen something like this:

    Movie Information
    ------------------------------
    Title: Seabiscuit
    minutes: 240

    Title: Identity
    minutes: 240

    Title: The Monster and the Girl
    minutes: 65

    Title: Chicago
    minutes: 160

    Title: DareDevil
    minutes: 167

    Title: Jurrasic Park
    minutes: 127

    ------------------------------

    but for some reason my program is not reading correctly and is printing out some bogus number...here is my program hope somebody can help me find a solution to this...

    Code:
    #include<iostream>	//for cout
    #include<fstream>	//for file I/O
    #include<string>	//for printing a string of words
    
    using namespace std;
    
    void PrintMovies(/* in */const string movieNames[], 
    				 /* in */const int movieMinutes[], 
    				 /* inout */int numOfMovies);
    
    int FindShortestMovie(const int movieMinutes[], int numOfMovies);
    
    const int MAX_SIZE = 10;	//constant variable declared 
    
    int main()
    {
    
    	string movieNames[MAX_SIZE];	//string array to store movie names
    	int movieMinutes[MAX_SIZE];		//int array to store movie minutes
    
    	int count;		//count variable to control loop
    	int numOfMovies;	//variable to read in the number of movies from input file
    
    	ifstream infile;	//variable for reading file
    	infile.open("movies.txt");	//opening file
    
    
    	cout<<"Movie Information"<<endl;		//prints to screen "Movie information"
    
    	//loop to print a line
    	for(int i = 0; i < 30; i++)
    		cout<<"-";
    
    	//adds a blank line after line is printed
    	cout<<endl;
    
    
    	if(!infile)		//checking to see if file is valid for opening
    	{
    		cout<<"Error reading File."<<endl;	//prints to screen "Error reading File" if file is invalid
    		return 1;
    	}
    
    	infile>>numOfMovies;	//reads in the number of movies from the input file
    
    	//loop reads in the information for all the movies and stores into parallel arrays
    	for(count=0; count < numOfMovies; count++)
    	{
    		getline(infile, movieNames[count]);
    		infile>>movieMinutes[count];
    		infile.ignore(1,'\n');
    	}
    
    	//prototype function call that prints out the name of the movies and the 
    	//minutes to the screen
    	PrintMovies(movieNames, movieMinutes, numOfMovies);
    
    	//loop to print a second line after information is printed
    	for(int y = 0; y < 30; y++)
    		cout<<"-";
    
    	//adds a blank line after line is printed
    	cout<<endl;
    
    	infile.close();	//closes file
    
    	return 0;
    }
    
    void PrintMovies(/* in */const string movieNames[], 
    				 /* in */const int movieMinutes[], 
    				 /* inout */int numOfMovies)
    {
    	//loop to print out all the information that is read and stored
    	for(int count=0; count < numOfMovies; count++)
    	{
    		cout<<"Title: "<<movieNames[count]<<endl;
    		cout<<"minutes: "<<movieMinutes[count]<<endl<<endl;
    	}
    
    }
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Your file has formatting, but your program is not handling the formatting correctly. Parsing input files is always a bit of a challenge and almost always needs to be done using custom code for each file type. I rewrote your routines so the would work using a buffer and some flags, run it in the debugger to see what is going on at each step. If the format of your input file changes (or gets corrupted), this code will fail, so it is best to put in error checking to gracelfully handle those conditions.

    Code:
    #include<iostream>	//for cout
    #include<fstream>	//for file I/O
    #include<string>	//for printing a string of words
    
    using namespace std;
    
    void PrintMovies(/* in */const string movieNames[], 
    				 /* in */const int movieMinutes[], 
    				 /* inout */int numOfMovies);
    
    int FindShortestMovie(const int movieMinutes[], int numOfMovies);
    
    const int MAX_SIZE = 10;	//constant variable declared 
    const int MAX_LINELENGTH = 1024;	//constant variable declared 
    
    int main()
    {
    
    	string movieNames[MAX_SIZE];	//string array to store movie names
    	int movieMinutes[MAX_SIZE];		//int array to store movie minutes
        char buffer[MAX_LINELENGTH];
    
    	int count, ptr;		//count variable to control loop
    	int numOfMovies;	//variable to read in the number of movies from input file
    
    	ifstream infile;	//variable for reading file
    	infile.open("movies.txt");	//opening file
    
    
    	cout<<"Movie Information"<<endl;		//prints to screen "Movie information"
    
    	//loop to print a line
    	for(int i = 0; i < 30; i++)
    		cout<<"-";
    
    	//adds a blank line after line is printed
    	cout<<endl;
    
    
    	if(!infile)		//checking to see if file is valid for opening
    	{
    		cout<<"Error reading File."<<endl;	//prints to screen "Error reading File" if file is invalid
    		return 1;
    	}
    
        count = 0;
        ptr = 0;
        while (infile.getline(buffer, MAX_LINELENGTH)){
            if (count == 0){
                numOfMovies = atoi(buffer);
            }else{
                if (count % 2 == 1){//this is a movie
                    movieNames[ptr] = buffer;
                }else{//this is a run length
                    movieMinutes[ptr] = atoi(buffer);
                    ptr++;
                }
                if (ptr > MAX_SIZE-1){
                    cerr << "More movies than MAX_SIZE!\n";
                    break;
                }
                if (ptr > numOfMovies){
                    cerr << "Unexpected number of movies!\n";
                    break;
                }
            }
            count++;
        }
    /*
    	infile>>numOfMovies;	//reads in the number of movies from the input file
    
    	//loop reads in the information for all the movies and stores into parallel arrays
    	for(count=0; count < numOfMovies; count++)
    	{
    		getline(infile, movieNames[count]);
    		infile>>movieMinutes[count];
    		infile.ignore(1,'\n');
    	}
    */
    	//prototype function call that prints out the name of the movies and the 
    	//minutes to the screen
    	PrintMovies(movieNames, movieMinutes, numOfMovies);
    
    	//loop to print a second line after information is printed
    	for(int y = 0; y < 30; y++)
    		cout<<"-";
    
    	//adds a blank line after line is printed
    	cout<<endl;
    
    	infile.close();	//closes file
    
    	return 0;
    }
    
    void PrintMovies(/* in */const string movieNames[], 
    				 /* in */const int movieMinutes[], 
    				 /* inout */int numOfMovies)
    {
    	//loop to print out all the information that is read and stored
    	for(int count=0; count < numOfMovies; count++)
    	{
    		cout<<"Title: "<<movieNames[count]<<endl;
    		cout<<"minutes: "<<movieMinutes[count]<<endl<<endl;
    	}
    
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    65
    Rep Power
    11
    thanks mikakeet, but I cannot really use that...my assigment requires me to use 3 "for loops" 1 for reading, 1 for printing the data on screen, and 1 for finding the shortest movie which I didnt write the code for yet...do you know how to modify my program without changing much...because my professor gave me guidlines to follow...thanks alot for you help tho...im still dont get why im not getting the right out put... i think some thing is wrong here:

    Code:
    infile>>numOfMovies;	//reads in the number of movies from the input file
    
    	//loop reads in the information for all the movies and stores into parallel arrays
    	for(count=0; count < numOfMovies; count++)
    	{
    		getline(infile, movieNames[count]);
    		infile>>movieMinutes[count];
    		infile.ignore(1,'\n');
    	}
    this area of code is supposed to read the file and collect all the data into memory but for some reason the "getline" function isnt working correctly, because its supposed to read the entire string, but it dont...seems like its not reading anything...I know the loop works when I debugged it...
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    You already have two loops, one for reading into memory and one for writing out to screen, all you got to do is get the third. You are having problems with your code because it is not consuming the trailing linefeed from your initial cin. There are probably millions of ways of doing this sort of thing and a large percentage of them are all perfectly acceptable. You are trying one way, I am trying another, your code has more opportunites for fatal errors, mine has fewer. You should never rely on your input file being in the correct format.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    65
    Rep Power
    11
    is there a way for me to convert your while loop into a for loop, because I havent really learned about "buffer" yet...
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    You can use a 'forever' loop, typically written this way:

    for (;;){

    }

    and then break out of it (it will run forever, hence its name) by testing for EOF.

    You could also do this:

    Code:
        if (infile.getline(buffer, MAX_LINELENGTH)){
            numOfMovies = atoi(buffer);
        }else{
            cout << "No data in file!\n";
            return 1;
        }
        for (count=0; count<numOfMovies; count++){
            infile.getline(buffer, MAX_LINELENGTH);
            movieNames[count] = buffer;
            infile.getline(buffer, MAX_LINELENGTH);
            movieMinutes[count] = atoi(buffer);
        }
    but there is no error checking and a small screwup in your data file can cause crashes. You could put in appropriate error handling if you like (there should be enough example code for you to work with) and have a perfectly acceptable method of parsing your input data.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    NY
    Posts
    18
    Rep Power
    0
    This should do the trick as well

    --messorian

    Code:
    #include<iostream>	//for cout
    #include<fstream>	//for file I/O
    #include<string>	//for printing a string of words
    
    using namespace std;
    
    // declare methods
    void PrintMovies(const string movieNames[], const int movieMinutes[], int numOfMovies);
    int FindShortestMovie(const int movieMinutes[], int numOfMovies);
    
    const int MAX_SIZE = 10;	//constant variable declared 
    
    // redifine atoi to use a more C++ like interface
    int atoi(const string& s) { 
    	return atoi(s.c_str());
    }
    
    
    int main() {
    	string buffer;
    	string movieNames[MAX_SIZE];	//string array to store movie names
    	int movieMinutes[MAX_SIZE];		//int array to store movie minutes
    
    	int count;						//count variable to control loop
    	int numOfMovies;				//variable to read in the number of movies from input file
    
    	ifstream infile;				//variable for reading file
    	infile.open("c:/temp/movies.txt");		//opening file
    
    	if(!infile) {					//checking to see if file is valid for opening
    		cout<<"Error reading File."<<endl;	//prints to screen "Error reading File" if file is invalid
    		return 1;
    	}
    
    	// read in the entire line and convert the value to an integer
    	getline(infile, buffer);
    	numOfMovies = atoi(buffer);
    	
    
    	// read in the movie names and times
    	for(count=0;count < numOfMovies; count++) {
    		// the name can be read directly into the movieNames array
    		getline(infile, movieNames[count]);
    		// read the time in and then convert it to an integer
    		getline(infile, buffer);
    		movieMinutes[count] = atoi(buffer);
    	}
    	
    	// the file can be closed now
    	infile.close();	
    
    	// print out the movies
    	PrintMovies(movieNames, movieMinutes, numOfMovies);
    	
    	// find the shortest movie and print it
    	cout << "The shortest movie is: " << movieNames[FindShortestMovie(movieMinutes, numOfMovies)] << endl;
    	
    
    
    	return 0;
    }
    
    // printMovies definition
    void PrintMovies(const string movieNames[], const int movieMinutes[], int numOfMovies) {
    	
    	//prints to screen "Movie information"
    	cout << "Movie Information" << endl;		
    	cout << "------------------------------ " << endl;
    
    	//loop to print out all the information that is read and stored
    	for(int count=0; count < numOfMovies; count++) 
    		cout << "Title: " << movieNames[count] << endl 
    			<< "minutes: " << movieMinutes[count] << endl << endl;
    
    }
    
    // find the shortest movie title
    int FindShortestMovie(const int movieMinutes[], int numOfMovies) {
    	int pos = 0;
    	for(int i = 1; i < numOfMovies; i++) 
    		if(movieMinutes[i] < movieMinutes[pos])
    			pos = i;
    
    	return pos;
    }

IMN logo majestic logo threadwatch logo seochat tools logo