Thread: Sorting problems.

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Unhappy Sorting problems.

    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:

    Code:
     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
    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 output

    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;

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1 work on your indentation
    2 do not use global vars
    3 where do you call your sort function?
    4 where do you output your data?
    5 do not use eof to control read loop - check the return value of getline
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Output? It doesn't look like that program produces any output. You're not calling your Sort function, and you've got no loop to output the results either.

    I suggest you desperately look into using an array of structures instead of multiple arrays.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. Sorting structures
    By RedRum in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2002, 12:19 PM