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

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    So I have ended up with this...
    Code:
    void Artist::FindCd()
    {
    	string find;
    
    		cout <<"\tEnter name of CD you want to find " << endl;
    		cout <<"\t            WARNING               " << endl;
    		cout <<"\t  THIS SEARCH IS CASE SENSITIVE   " << endl;
    		cout <<"Enter name of CD now : " << flush;
    		cin >> find;
    
    	list<CdInfo>::iterator iter ;
    
    		for(iter = myCdInfo.begin(); iter != myCdInfo.end(); iter++)
    		{
    			if ((*iter).getCdName() == find)
    			{
    				cout << endl << "You have just found : " << (*iter).getCdName() << endl;
    			}
    		}		
    			
    }
    Everything is compiling correct but when I go to use the find function it does not do what I want it to do (display the information). Better yet it is not executing the body of the if loop.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That code looks fine. How and where are you calling the function?

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Well I guess the only way to show you this is to post a huge chunk of my prog.....
    This is my Controlle Class...
    Code:
    #include "CdInfo.h"
    #include "Controller.h"
    #include "Artist.h"
    #include "Menu.h"
    #include "SubMenu.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    //*********************MAIN*CONTROL********************
    //Takes input from user and processes it
    void Controller::CommandControl(int input){
    	Menu menu;
    	Artist artist;
    
    
    	if (input == 1)
    	{
    		menu.Info(); //Displays info about program
    	}
    	else if (input == 2)
    	{
    		CdInfoMenu();
    	}
    	else if (input == 3)
    	{
    		cout << "Goodbye" << endl; //End Program
    	}
    	else if (input == 4)
    	{
    		DisplayArtistList();
    	}
    	else
    	{
    		cout << input << ":  is not a valid command" << endl;	
    	}
    }
    
    //*******************CD*INFO**********************
    
    //Takes input from user (CdInfo) and processes it
    void Controller::CdInfoMenu(){
    	int input;
    	Menu menu;
    	Artist artist;
    	SubMenu submenu;
    
    		submenu.CdInfo();
    
    		cout << endl << "Enter your choose from the Cd Info Menu now:" << flush ;
    		cin >> input;
    		cout << endl;
    		if (input == 1)
    		{
    			artist.AddCdName();
    			myArtist.push_back(artist);	
    		}
    		else if (input == 2)
    		{
    			DisplayArtistList();
    		}
    		else if (input == 3)
    		{
    			artist.FindCd();
    		}
    		else if (input == 4)
    		{
    			menu.MainMenu();
    		}
    		else 
    		{
    			cout << input << ": is not a valid command" << endl;
    		}
    }
    
    //********************ARTIST**********************
    
    //Adds Artist Name
    void Controller::AddArtBandName() { 
    	string artName; 
    
    		cout << endl << "Enter the bands name: " << flush; 
    		cin >> artName; 
    
    		myArtist.push_back(Artist(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();
    		}
    }
    //Not yet working
    void Controller::FindCd()
    {
    	string find;
    
    		cout <<"\tEnter name of Artist you want to find " << endl;
    		cout <<"\t               WARNING                " << endl;
    		cout <<"\t     THIS SEARCH IS CASE SENSITIVE    " << endl;
    		cout <<"Enter name of Artist now : " << flush;
    		cin >> find;
    		
    	list<Artist>::iterator iter ;
    
    		for(iter = myArtist.begin(); iter != myArtist.end(); iter++)
    		{
    			if ((*iter).getArtBandName() == find)
    			{
    				cout << endl << "You have just found : " << (*iter).getArtBandName() << endl;
    			}
    		}			
    		
    }
    Controller::Controller(){
    }
    This is my Artist class
    Code:
    #include "Artist.h" 
    #include "Controller.h"
    #include <iostream> 
    #include <string> 
    #include "Controller.h"
    
    // Constructor
    Artist::Artist(string lv_artName) 
    { 
    artName = lv_artName; 
    }
    
    //Default Constructor
    Artist::Artist(void)
    {
    }
    
    //Holds artName
    void Artist::setArtBandName(string lv_artName) 
    { 
    artName = lv_artName; 
    } 
    
    string Artist::getArtBandName() 
    { 
    return artName; 
    }
    
    
    //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();		
    		}
    }
    
    void Artist::OptionArtistInfo(){
    	 int input;
    
    	//A do while loop might be needed 
    			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;
    			}
    }
    
    void Artist::FindCd(string find){
    string find;
    
    		cout <<"\tEnter name of CD you want to find " << endl;
    		cout <<"\t            WARNING               " << endl;
    		cout <<"\t  THIS SEARCH IS CASE SENSITIVE   " << endl;
    		cout <<"Enter name of CD now : " << flush;
    		cin >> find;
    		
    		list<CdInfo>::iterator iter ;
    
    		for(iter = myCdInfo.begin(); iter != myCdInfo.end(); iter++)
    		{
    			if ((*iter).getCdName() == find)
    			{
    				cout << endl << "You have just found : " << (*iter).getCdName() << endl;
    			}
    		}				
    			
    }
    Last edited by chadsxe; 07-16-2005 at 12:31 PM.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Check out the second paragraph in my very first post in this thread. That is how you should implement FindCd. You should be putting the user input and the output in the Controller::FindCd function. You should pass a string as a parameter to the Artist::FindCd function, and return true or false whether the Cd was found.

    The reason you are getting nothing is because you have an empty Artist variable called artist. That variable is created on the stack, so it is empty every time you call CdInfoMenu. If the user chooses option 3, then you are looking up the Cd in an empty artist.

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Take a look at the code I posted last. I just edited with the the changes you suggested. I think I am doing it correct but now I am getting error

    error C2660: 'Artist::FindCd' : function does not take 0 arguments

    This error is pointing to the CdInfoMenu funcition in the controller class. More so this

    Code:
    else if (input == 3)
    		{
    			artist.FindCd();
    		}

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Think about it. I don't want to tell you exactly what should be done. The controller has a list of artists, and you want to go through each one of those artists looking for the cd. Walk through how the code will execute in your mind or on paper. Start with the CdInfoMenu function and imagine the user enters 3. Then just follow the code and see where it goes differently than where you want it to go.

  7. #22
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    I understand your point about not wanting to tell me. Trust me I don't want you to tell me, I much rather figure it out myself. I am just getting frustrated and need calm down and retrack my steps. Also, I can't thank you enough for all the help you have giving me so far. Sometimes you can only learn so much by reading books and tutorials. Actually talking to someone about specific problems has been great.

  8. #23
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Boy oh Boy......

    I have a thing or 2 to learn of the flow of controll in programming. So I spent the last few hours reading over your post trying to figure it while trying to go through my program step by step. If you look at the code that I posted a few post back, you will see the recent changes I have made. Hear are a few things that are still confusing me. When the user wants to find a CD (which will also find the Artist), do I call the FindCd function in my controller class first or my Artist class first? From there I know my loop has to search the list to find that instance of the user input. Then do I have to pass that instances over to my other other FindCD function?

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