Thread: How do you search a list container....

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    How do you search a list container....

    I have a program that is a crude CD database. It first asks the user to enter a CD name in which it stores into a list, then at the users on accord they can enter an artist name into another list that is a list of the CD name. So its a list of a list. I think I have it working correct but I am always open for opinions on improvment. Anyways what I am working on at the moment is giving the user the option to search out a CD from the list and display back that CD name and artist. Hear is what I have so far..

    This is in a Class called Controller
    Code:
    //Adds Artist Name
    void Controller::AddArtBandName() { 
    	string artName; 
    
    		cout << endl << "Enter the bands name: " << flush; 
    		cin >> artName; 
    	
    		myArtist.push_back( (artName));	
    		
    		cout << "\tAdded : " << artName << endl << endl ;	
    } 
    
    //Displays Artist List 
    void Controller::DisplayArtistList(){
    		cout << endl << "List of Artist: " << endl;
    
    	list<Artist>::iterator iter;
    
    		for(iter = myArtist.begin(); iter != myArtist.end(); iter++)
    		{
    		cout << (*iter).getArtBandName()<<endl;
    		(*iter).DisplayCdList();
    		}
    }
    void Controller::FindCd(){
    	string search;
    
    		cout <<" Enter name of CD you want to find " << endl;
    		cout <<"             WARNING               " << endl;
    		cout <<"   THIS SEARCH IS CASE SENSITIVE   " << endl;
    		cin >> search;
    
    }
    this is in a class called Artist
    Code:
    //Adds Cd Name
    void Artist::AddCdName(){
    	string cdName; 
    
    		cout << "Enter Cd's Name:" << flush; 
    		cin >> cdName; 
    
    		myCdInfo.push_back(CdInfo(cdName));	
    
    		cout << "\tAdded : " << cdName << endl << endl;		
    	OptionArtistInfo();
    }
    
    //Display Cd List
    void Artist::DisplayCdList(){
    		cout << endl << "Cd List:" << endl;
      
    	list<CdInfo>::iterator iter;
    
    		for(iter = myCdInfo.begin(); iter != myCdInfo.end(); iter++)
    			
    			cout << (*iter).getCdName() << endl;		
    }
    
    void Artist::OptionArtistInfo(){
    	 int input;
    
    			cout << "Would you like to add Artist info for the cd : " << endl; 
    			cout << "\tType 1 for yes or 2 for no : " << flush;
    			cin >> input;
    		
    			switch (input)
    			{
    			case 1:
    					cout << endl << "Enter the bands name: " << flush; 
    					cin >> artName; 
    				break;
    			case 2:
    				cout << "Returning to main menu" << endl;
    				break;
    			default:
    				cout << input << " : Is not a valid command" << endl;
    				cout << " 1 and 2 are valid commands " << endl;
    			}
    }
    The FindCd function in the Controller class is were I am trying to search through the Cd list and find a Cd based on the users input. For now I just want it to display back that Cd and the Artist Name associated with it. To be honest I have no clue how to go about this. I am guessing a for if loop of some sorts. Can anyone help put me on the correct path.....

    Thanks

    Chad
    Last edited by chadsxe; 07-14-2005 at 10:35 AM. Reason: Updated code.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  3. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM