This code is meant to sort a list of movies from a file based on title, year, media and length. The format for the movie file is:
I'm looking to figure out what it is that I'm doing wrong. It outputs the first title many times. It looks like a loop problem but I can't figure it out. I don't expect this program to work magically but it seems I just can't find the problem. It outputs over and over again and I'm really not seeing the logic that causes it to run without more accurate output. I really need to figure out why it doesn't outputCode:You've Got Mail,1998,109,DVD Birthday Girl,2002,129,VHS Get Shorty,1995,117,DVD Spy Game,2001,120,DVD About a Boy,2002,117,DVD
Code:#include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; //Constants - This is used inside sort function to determine //which field to sort on const int MOVIE_TITLE = 0; const int YEAR = 1; const int MEDIA = 2; const int LENGTH =3; //Global Variables string title[188]; string year[188]; string mediaType[188]; string length[188]; void Sort(const int col, int howMany, bool dir) { switch(col) { case MOVIE_TITLE: { string temp_title; string temp_year; string temp_mediaType; string temp_length; int limit = howMany;//The number of elements in the array to be searched bool swapped = true; int j = 0; while(swapped) { /*j++; cout << "iteration "<<j << endl; */ swapped = false;//No swapp has taken place //Propagate through array for(int sub=0;sub<limit - 1; sub++) { //dir - asc, or desc order, nothing to do with files if(dir) { //ASCENDING if(title[sub] > title[sub+1]) { //elements are out of order..swap them //Write first to temp temp_title = title[sub]; temp_year = year[sub]; temp_mediaType = mediaType[sub]; temp_length = length[sub]; //Overwrite first with second title[sub] = title[sub+1]; year[sub] = year[sub+1]; mediaType[sub] = mediaType[sub+1]; length[sub] = length[sub+1]; //Overwrite second with temp title[sub+1] = temp_title; /*cout << "TRACER2 = "<< title[sub+1] << endl<<cin.get();*/ year[sub+1] = temp_year; mediaType[sub+1] = temp_mediaType; length[sub+1] = temp_length; swapped = true; } } else { //DESCENDING if(title[sub] < title[sub+1]) { //elements are out of order..swap them string temp_title = title[sub]; string temp_year = year[sub]; string temp_mediaType = mediaType[sub]; string temp_length = length[sub]; title[sub] = title[sub+1]; year[sub] = year[sub+1]; mediaType[sub] = mediaType[sub+1]; length[sub] = length[sub+1]; title[sub+1] = temp_title; year[sub+1] = temp_year; mediaType[sub+1] = temp_mediaType; length[sub+1] = temp_length; swapped = true; } } }//end for() limit--; }//end while() break; } case YEAR: break; case MEDIA: break; case LENGTH: break; } } int main() { //Declare vars //File record //int age; //int code; //Generalg string record; int recordcount = 0; // int maxElements; ifstream dataIn; //Search // string lookFor; // int compares = 0; //Open file dataIn.open("Movies.dat"); if(dataIn.fail()) { cout << "Unable to open the data file." << endl; return 99; } //Read a record getline(dataIn,record); int delimiterposition; //location in the record of field separator (,) recordcount = 0; while(dataIn.eof() == false) { if(record == "") { break; } delimiterposition = record.find_first_of(',',0); /*cout << "record="<<record<< endl;*/ if(record.substr(0,delimiterposition-0) == "") { break; } title[recordcount] = record.substr(0,delimiterposition-0); delimiterposition = record.find_first_of(',',0); year[recordcount] = record.substr(0,delimiterposition-0); /*cout<< "TRACER3 year="<<year<<endl<<cin.get();*/ delimiterposition = record.find_first_of(',',0); mediaType[recordcount] = record.substr(0,delimiterposition-0); delimiterposition = record.find_first_of(',',0); length[recordcount] = record.substr(0,delimiterposition-0); recordcount++; //point to next element getline(dataIn,record); }//end of input loop return 0;



LinkBack URL
About LinkBacks


