Please check my C++

This is a discussion on Please check my C++ within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by matsp By the way, here's a post I made a while back with regards to creating a ...

  1. #226
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    By the way, here's a post I made a while back with regards to creating a menu system - perhaps you can use it as a base:
    Menu (any default style ? )

    --
    Mats
    I'm trying to understand your code, well, i've never worked on linked list but have an idea..

    Code:
    // Class menucontext
    class menucontext 
    {
    public:
    	bool fquit;
    	menucontext() : fquit(false) {}
    	~menucontext() {};
    };
    typedef void (*menufunc)(menucontext &ctxt);
    Could you explain the purpose of this function pointer menufunc & class menucontext? Expanding to that, how/for what reasons it's used here in"domenu"?

    Code:
    		for(p = head; p; p = p->next) 
    		{
    			if (p->item->isChoice(c))
    			{
    				p->item->domenu(ctxt);
    				choosen = true;
    			}
    		}
    
    .............
    
    void menuitem::domenu(menucontext &ctxt) 
    {
    	func(ctxt);
    }

  2. #227
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The context is there so that you can have some data [that is not global] that you can pass between the function that uses the menu class itself, and the functions provided as the menu functions. This class probably would need to be expanded - right now it only contains a flat to indicate if we want to quit or not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #228
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    The context is there so that you can have some data [that is not global] that you can pass between the function that uses the menu class itself, and the functions provided as the menu functions. This class probably would need to be expanded - right now it only contains a flat to indicate if we want to quit or not.

    --
    Mats
    In simple terms, could you explain this for loop
    Code:
    		for(p = head; p; p = p->next) 
    		{
    			if (p->item->isChoice(c))
    			{
    				p->item->domenu(ctxt);
    				choosen = true;
    			}
    		}
    thnx

  4. #229
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The for loop walks the linked list, from head until it finds a 0 pointer. It checks if the choice is matching the current menuitem, and if so, calls the menu for that and sets the "isChoosen" variable [for optimization, on long menus, you may want to change the ; p ; in the for-loop into ; p && !isChoosen;

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #230
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Understanding link lists

    Quote Originally Posted by matsp View Post
    The for loop walks the linked list, from head until it finds a 0 pointer. It checks if the choice is matching the current menuitem, and if so, calls the menu for that and sets the "isChoosen" variable [for optimization, on long menus, you may want to change the ; p ; in the for-loop into ; p && !isChoosen;

    --
    Mats
    To help myself understand link lists, i'm gonna try explain this function and stand for corrections

    Code:
    void menu::addmenu(std::string s, char c, menufunc f) 
    {
    	menunode *node = new menunode;
    	node->item = new menuitem(s, c, f);
    	node->next = 0;
    
    	if (!head)
    		head = tail = node;
    	else 
    	{
    		tail->next = node;
    		tail = node;
    	}
    };
    If called for the first time, head and tail will be NULL, thus item will be contained at the head and tail after the 'if' execution. If called the second time where both head & tail contain same data, tail will now point to the new data (node) hence the 'else' part of the if statement.

    Confusions
    1. tail->next = node points to node already, why tail=node
    2. If called for the third time, you replace tail with new node. What happens to what tail pointed to before (second time)? I know you inserting new item at the end.. can't see the logic of that though!
    Last edited by csonx_p; 07-23-2008 at 05:51 AM.

  6. #231
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In a linked list, you often only track the head and/or tail - the other components are held together with the links within the list. So, if we have 3 elements (1, 2, 3) in a linked list:
    head->1->2->3 <- tail

    So tail points to the currently last element in the list at the end of the function. When the first element is inserted, obviously the first AND the last element are the same one, so tail and head point to the same thing. Once we start adding more element, tail will point to the most recently added [since we don't do any sorting or such - elements are in the order they were inserted].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #232
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    In a linked list, you often only track the head and/or tail - the other components are held together with the links within the list. So, if we have 3 elements (1, 2, 3) in a linked list:
    head->1->2->3 <- tail

    So tail points to the currently last element in the list at the end of the function. When the first element is inserted, obviously the first AND the last element are the same one, so tail and head point to the same thing. Once we start adding more element, tail will point to the most recently added [since we don't do any sorting or such - elements are in the order they were inserted].

    --
    Mats
    mind you the if/else you have is only used to point, not to insert... Maybe that's where my confusion was!

  8. #233
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    mind you the if/else you have is only used to point, not to insert... Maybe that's where my confusion was!
    Well, the "insert" in a linked list is just a "link it in", which is done in the else-part. In the initial link, there is nothing other than the head to "link in" - and the next link is NULL, which is already done above the if-else (since we ALWAYS insert at the end, next is always NULL on a new node). The else part sets the current tail's next pointer to the new node, then moves the tail to the new last element.
    Insert element 3 in a 1, 2 list:
    Code:
    before:
    head -> 1
            |
            v
    tail -> 2
            |
            v
           NULL
    after tail->next = node:
    head -> 1
            |
            v
    tail -> 2
            |
            v
            3
            |
            v
           NULL
    after tail = node:
    head -> 1
            |
            v
            2
            |
            v
    tail -> 3
            |
            v
           NULL
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #234
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Well, the "insert" in a linked list is just a "link it in", which is done in the else-part. In the initial link, there is nothing other than the head to "link in" - and the next link is NULL, which is already done above the if-else (since we ALWAYS insert at the end, next is always NULL on a new node). The else part sets the current tail's next pointer to the new node, then moves the tail to the new last element.
    Insert element 3 in a 1, 2 list:
    Code:
    before:
    head -> 1
            |
            v
    tail -> 2
            |
            v
           NULL
    after tail->next = node:
    head -> 1
            |
            v
    tail -> 2
            |
            v
            3
            |
            v
           NULL
    after tail = node:
    head -> 1
            |
            v
            2
            |
            v
    tail -> 3
            |
            v
           NULL
    --
    Mats
    makes sense, thanx...

    Lastly on this topic...
    This class (menucontext) probably would need to be expanded - right now it only contains a flag to indicate if we want to quit or not.
    What do you imagine should be added as a member to this class... Say i have 5 Menu Items...

  10. #235
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    makes sense, thanx...

    Lastly on this topic...


    What do you imagine should be added as a member to this class... Say i have 5 Menu Items...
    It depends very much on how you want to design the user-interface for your system. I can imagine something like this for renting a car out:
    * Register new hirer (or find existing).
    * Find suitable car
    * finalize contract

    If we do it this way, when you register new hirer, you need to remember that hirer for when you finalize the contract. Likewise for the car. Once you have issued the contract, you probably want to "forget" what the current hirer and car is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #236
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    It depends very much on how you want to design the user-interface for your system. I can imagine something like this for renting a car out:
    * Register new hirer (or find existing).
    * Find suitable car
    * finalize contract

    If we do it this way, when you register new hirer, you need to remember that hirer for when you finalize the contract. Likewise for the car. Once you have issued the contract, you probably want to "forget" what the current hirer and car is.

    --
    Mats
    Am not sure if you understood me, take for example..

    Code:
    		case ADDCAR:	AddNewCar();	// Add new car
    						break;
    			
    		case UPDATEF:	UpdateFleetFile();	// Save changes to text file
    						break;
    		
    		case EDITF:		EditFleetContract();	// Edit contract details
    						break;
    		
    		case DELCAR:	DeleteCar(); // Remove a specific car from fleet
    						Pause();
    This is what needs to be part of Menu, or currently is... But, the class 'menucontext' has fquit as the only functionality, to help quit the program. My question is how do i relate the above options/functionalities with this class, if at all. My knowledge is i have to pass all these functions above any many others using the addmenu() function, which inserts them to the list or table. So, where does menucontext come in? or of what help except 'Quitting' would it be! Hope am clear....

  12. #237
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If all you "need" from one menu to another is to know if you wanted to quit or not, then fQuit is all you need in menucontext. If on the other hand you would like to remember something from one menu selection to another (for example the car you select in "find car"), then you may want to add that to the menucontext.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #238
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    If all you "need" from one menu to another is to know if you wanted to quit or not, then fQuit is all you need in menucontext. If on the other hand you would like to remember something from one menu selection to another (for example the car you select in "find car"), then you may want to add that to the menucontext.

    --
    Mats
    mhh... notice menufunc() has menucontext as a ref parameter... How do i pass a function with a different parameter, and a none void return value? Let me guess, this is going back to what you said (modifying menucontext for all functions ) ... Well, let take one function ..

    Code:
    bool ShowFleetList(vector<car>::iterator i , bool status);
    How would i cater for this?

  14. #239
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you currently pass to ShowFleetList? [that is, what does the code look like in the calling place].

    If the vector of cars is a global, then you don't need to pass anything. If it's not a global, then you need some way to pass it along, and menucontext would be one way to achieve that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #240
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    What do you currently pass to ShowFleetList? [that is, what does the code look like in the calling place].
    Mats
    Code:
    FleetIterator iterate;
    ShowFleetList(iterate=fleet.begin(), found);
    only fleet is global...

    If the vector of cars is a global, then you don't need to pass anything.
    They may be other functions which receives parameters, and return values

    If it's not a global, then you need some way to pass it along, and menucontext would be one way to achieve that.
    one example would help, maybe based on the above function...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-06-2010, 12:42 AM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 08:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 11:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2002, 12:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21