Page 4 of 4 First ... 234
  • Jump to page:
    #46
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    This is how the output is expected to look like:
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    Please enter name and priority
    Michel 6
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    please enter name and priority
    David 2
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    Please enter name and priority
    Jeff 3
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    Please enter name and priority
    John 3
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [Michel Jeff John David ]
    Orders: []
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    2
    Is the driver specific (please enter 1/0)?
    1
    Please enter the driver's name
    Jeff
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [Michel John David ]
    Orders: []
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    2
    Is the driver specific (please enter 1/0)?
    1
    Please enter the driver's name
    Michael
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [Michel John David ]
    Orders: [2 ]
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    2
    Is the driver specific (please enter 1/0)?
    0
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [John David ]
    Orders: [2 ]
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    Please enter name and priority
    Michael 2
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [John David ]
    Orders: []
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    4
    Press any key to continue . . .
  2. #47
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by peripatein
    This is how the output is expected to look like:
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    Please enter name and priority
    Michel 6
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    please enter name and priority
    David 2
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    Please enter name and priority
    Jeff 3
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    Please enter name and priority
    John 3
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [Michel Jeff John David ]
    Orders: []
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    2
    Is the driver specific (please enter 1/0)?
    1
    Please enter the driver's name
    Jeff
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [Michel John David ]
    Orders: []
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    2
    Is the driver specific (please enter 1/0)?
    1
    Please enter the driver's name
    Michael
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [Michel John David ]
    Orders: [2 ]
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    2
    Is the driver specific (please enter 1/0)?
    0
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [John David ]
    Orders: [2 ]
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    1
    Please enter name and priority
    Michael 2
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    3
    Drivers: [John David ]
    Orders: []
    Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)exit
    4
    Press any key to continue . . .
    I don't really understand two things: 1) The queues are expected to be displayed based on the following format: Drivers: [Driver 1 Driver 2 Driver 3] Orders: [orderID1 orderID2 ]. And it is stated that the orderID, which is an integer in the TaxiSystem main struct, is assigned by the system. What I do not understand is how that ID is actually assigned and why are there only two IDs for three drivers? 2) Based on the format of the output in the previous post, why is the drivers' queue not updated near the end of that output, right after Michael 2? Why does the output still read [John David ]?
  4. #48
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    So what happens when you make that correction in Msg #45?
  6. #49
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    The program now runs nearly perfectly. I thank you all wholeheartedly! There is one more thing I am concerned about, namely the instruction to verify that the input of the drivers' names does not exceed a 100 chars, and if it does then the chars should be passed to the next string. How do I go about that? Does it suffice to merely write something like %s100? I am really not sure. Any suggestions, please?
  8. #50
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Actually, I'm not a good one to ask. Most of my work in C has been in embedded programming, so I never use console I/O functions (I know a lot about printf because we use sprintf to create strings). I would recommend that you start a new topic with this question, since asking it here is not as visible to others who know more than I do.

    As I understand, you can indeed limit the number of characters input by scanf by using "%100s" (note that field widths go between the percent sign and the format flag). However, I do not know how to handle your question about reading in the remaining characters "to the next string" (there's a "next string"?).

    Post a new topic to elicit more answers and better informed answers.
  10. #51
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Hi,
    I have one final question concerning my program. How should I free my main struct TaxiSystem at the end of the program? It was initialized thus:
    Code:
    TaxiSystem* initSystem(void){
        TaxiSystem *new_node = NULL;
        new_node = (TaxiSystem*) malloc(sizeof(TaxiSystem));
        if (!new_node){
            printf("Fatal error: memory allocation failed!\n");
            return NULL;
        }
        new_node->numOrder = 0;
        new_node->driversQueue = NULL;
        new_node->ordersQueue = NULL;
        return (new_node);
    }
    And main() also called some other functions to create new orders and new drivers in their respective queues using malloc().
    I was wondering whether simply doing:
    Code:
    free(sysptr)
    /*where sysptr is defined thus TaxiSystem *sysptr = initSystem();*/
    could suffice? The program runs smoothly and never gives any error messages nor gets stuck. Yet I wasn't sure whether freeing TaxiSystem this way would be enough or whether I also had to iterate over the inner structs too etc.?
    I'd truly appreciate some advice.
  12. #52
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    First you need to walk through both queues and free each individual node there. And during normal operation, whenever you remove and dispose of each node in a queue, you need to free it.

    You won't see the effects of a memory leak in such a small program nor in one that fails to clean up after itself just before termination, because all its memory gets reclaimed by the OS after termination. But you need learn the discipline for when you are writing professional programs that must run error-free for extended periods of time, usually providing vital services.
  14. #53
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by dwise1_aol
    First you need to walk through both queues and free each individual node there. And during normal operation, whenever you remove and dispose of each node in a queue, you need to free it.

    You won't see the effects of a memory leak in such a small program nor in one that fails to clean up after itself just before termination, because all its memory gets reclaimed by the OS after termination. But you need learn the discipline for when you are writing professional programs that must run error-free for extended periods of time, usually providing vital services.
    Will this be sufficient?:
    Code:
    while (system->driversQueue){
    		free(system->driversQueue);
    		system->driversQueue = system->driversQueue->next;
    	}
    	while (system->ordersQueue){
    		free(system->ordersQueue);
    		system->ordersQueue = system->ordersQueue->next;
    	}
    	free(system);
  16. #54
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    No! You are immediately dropping your pointers! First save the next pointer in a temporary pointer variable, then free the node.

    Work it out with pencil and paper!
  18. #55
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    How about that?:
    Code:
    void freeSystem(TaxiSystem* system){
    	while (system->driversQueue){
        	void* tmp = system->driversQueue->next;
        	free(system->driversQueue);
        	system->driversQueue = tmp ;
    	}
    	while (system->ordersQueue){
    		void* tmp = system->ordersQueue->next;
        	free(system->ordersQueue);
        	system->ordersQueue = tmp ;
    	}
    	free(system);
    }
  20. #56
  21. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Why a void pointer? You know what datatype each node is. I guess that would work. I would never have considered using a void pointer, so I have no experience to inform me of any potential problems.

    And why redeclare it each and every time you go through the loop?
  22. #57
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by dwise1_aol
    Why a void pointer? You know what datatype each node is. I guess that would work. I would never have considered using a void pointer, so I have no experience to inform me of any potential problems.

    And why redeclare it each and every time you go through the loop?
    Is it better now?:
    Code:
    Void* tmp = NULL;
    	while (system->driversQueue){
        	tmp = system->driversQueue->next;
        	free(system->driversQueue);
        	system->driversQueue = tmp ;
    	}
    	while (system->ordersQueue){
    		tmp = system->ordersQueue->next;
        	free(system->ordersQueue);
        	system->ordersQueue = tmp ;
    	}
    	free(system);
Page 4 of 4 First ... 234
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo