Thread: Rolodex(information manager) =>Check my Code

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    72

    Rolodex(information manager) =>Check my Code

    here's what i should do:

    Provide the following interactive commands in main() to work with your rolodex:

    * list - displays all the cards in the rolodex, in alphabetical order from beginning to end.
    * view - displays the card at the current position.
    * flip - updates the current position to the next card, and displays it. Flipping past the last card wraps around to the first card.
    * search - finds and displays a card, and makes it the current position in the rolodex. This command prompts for the last name for searching. If a matching card is found, it is displayed and is set as the current position. If no matching card is found, the card that immediately follows the lookup name is displayed and set as the current position (e.g. if "H" is entered as the last name, the first card with a last name following "H" in alphabetical order is displayed). If there is no following card, displays "Couldn't find: name"
    * add - adds a new card to the rolodex. Prompts for each field value, reads it, and enters the new card in the correct position in the rolodex (based on alphabetical order). The new card is set as the current position.
    * remove - removes the card at the current position. It displays the card and prompts for a confirmation for removal. The following card is set as the current position.
    * modify - updates the card at the current position. Enters a mode that requests the field to be updated (e.g. phone #), displays the existing value and prompts for the new value, reads it, and updates the card. Continues prompting for additional changes until all changes are done (e.g. a 0 is entered for the field # to change). If the last name is changed, the card must be moved to the correct position in the rolodex (and is set as the current position).
    * quit - exits the program.

    * The add() member function should have a Card parameter, so that the main() code adding a card first does the interaction with the user to get the info for the new card, constructs the Card object, and then finally calls the Rolodex.add() method, passing the Card object to be added to the rolodex. The Rolodex.add() method shouldn't do all the interaction with the user or create the Card (that's done by the caller). It just adds its parameter Card object to the Rolodex collection of cards (in the correct position), and updates its iterator to refer to the new card as the current position.
    * The list() member function should use a local iterator to iterate through the collection of cards and display them. It can have an ostream parameter that the output should be displayed to, and can call each card's display member function for the output. It doesn't know how to display the card's data, rather, it requests each card to display its data on the provided ostream parameter.
    * A modify can be done by the code in main() getting the current card, performing the interactions to modify its value(s), and then if the modify is approved by the user, call the Rolodex.remove() function to remove the (old) card from the collection, and then Rolodex.add() to add the modified card to the collection. Here again, the Rolodex class knows nothing about how to modify the contents of a card. Most of the work is done in the main() function that interacts with the user, and then the Rolodex functions are used to simply remove the old card and add back the modified card (which may have a changed last name, so it would be added back at a different position in the collection).

    Code:
    //
    #include <iterator>
    #include "Rolodex.h"
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::ostream;
    #include <map>
    #include <string>
    using std::string;
    #include <algorithm>
    
    
    Rolodex::Rolodex()
    {
      
    }
    
    Rolodex::~Rolodex()
    {} //end dtor
    
    void Rolodex::List()
    {
    	for ( iter = rolodex.begin(); iter != rolodex.end(); iter++ )
    		cout << (*iter).second.cardinfo() << endl; 
    }//end List function
    
    void Rolodex::View()
    {	
    	iter = rolodex.begin();
    	cout << (*iter).second.cardinfo() << endl; //show current position
    
    }//end View function
    
    void Rolodex::Flip()
    {
    	iter = rolodex.upper_bound(iter->first);
    	cout << (*iter).second.cardinfo() << endl;
        
    }
    
    void Rolodex::Add(Rolocard& newcard)
    {
    	iter = rolodex.insert(iter->first, newcard);
    }
    
    void Rolodex::Search()
    {
    	string last;
    	cout << "Enter last name :          ";
    	cin >> last;
    	iter = rolodex.find(last);
    }
    
    void Rolodex::Remove()
    {	
    	std::multimap < string, Rolocard >::iterator temp;
    	string answer;
    	cout << "Do you want to remove card? Y/N ? " ;
    	cin >> answer;
    	if ( answer == "Y" )
    	iter = temp;
    	rolodex.erase(temp);
    	iter = rolodex.upper_bound(iter->first);	
    }
    
    void Rolodex::Modify(Rolocard& newcard)
    {	
    	Remove(newcard);
    	Add(newcard);
    }
    What's wrong with my code, its a multimap.

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You gave us the expected output but I don't see the output that you are getting (as I am not keen on writing the code to test this).

    You should say what is going on and why you suspect/know something is not functioning correctly.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Here's the class
    Code:
    //Rolodex.h
    //Declaration of class Rolodex
    
    #ifndef ROLODEX_H
    #define ROLODEX_H
    
    #include <map>
    #include <iterator>
    #include <algorithm>
    #include <string>
    using std::string;
    using namespace std;
    #include "Rolocard.h"
    
    class Rolodex
    {
    
    public:
    		Rolodex();
    		~Rolodex();
    		void List();
    		void View();
    		void Flip();
    		void Search();
    		void Add(Rolocard&);
    		void Remove();
    	    void Modify(Rolocard&);
    		void Quit();
    
    private:
    	std::multimap < string, Rolocard > rolodex; 
    	std::multimap < string, Rolocard  >::iterator iter;
    	
    	
    
    }; //end of decl. of class Rolodex
    
    #endif
    1>------ Build started: Project: Rolodex, Configuration: Debug Win32 ------
    1>Compiling...
    1>Rolodex.cpp
    1>.\Rolodex.cpp(45) : error C2664: 'std::_Tree<_Traits>::iterator std::multimap<_Kty,_Ty>::insert(std::_Tree<_Traits >::const_iterator,const std:air<_Ty1,_Ty2> &)' : cannot convert parameter 1 from 'const std::string' to 'std::_Tree<_Traits>::const_iterator'
    1> with
    1> [
    1> _Traits=std::_Tmap_traits<std::string,Rolocard,std ::less<std::string>,std::allocator<std:air<const std::string,Rolocard>>,true>,
    1> _Kty=std::string,
    1> _Ty=Rolocard,
    1> _Ty1=const std::string,
    1> _Ty2=Rolocard
    1> ]
    1> and
    1> [
    1> _Traits=std::_Tmap_traits<std::string,Rolocard,std ::less<std::string>,std::allocator<std:air<const std::string,Rolocard>>,true>
    1> ]
    1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    1>.\Rolodex.cpp(70) : error C2660: 'Rolodex::Remove' : function does not take 1 arguments
    1>main.cpp
    1>.\main.cpp(18) : error C2447: '{' : missing function header (old-style formal list?)
    1>Generating Code...
    1>Build log was saved at "file://c:\Users\Fresh2Def\Documents\Visual Studio 2008\Projects\Rolodex\Rolodex\Debug\BuildLog.htm"
    1>Rolodex - 3 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    it is suppose to manage the cards. I'm using associative container Multimap to hold the cards. the key is the last name and the other pair is the card(class Rolocard).

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    1>.\Rolodex.cpp(45) : error C2664: 'std::_Tree<_Traits>::iterator std::multimap<_Kty,_Ty>::insert(std::_Tree<_Traits >::const_iterator,const std:air<_Ty1,_Ty2> &)' : cannot convert parameter 1 from 'const std::string' to 'std::_Tree<_Traits>::const_iterator'
    If you look at the references of multimap, you'd see there's no version of insert that takes key_type and mapped_type as separate arguments.

    Code:
    1>.\Rolodex.cpp(70) : error C2660: 'Rolodex::Remove' : function does not take 1 arguments
    
    void Remove();
    
    void Rolodex::Modify(Rolocard& newcard)
    {	
    	Remove(newcard);
    	Add(newcard);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM