Thread: another c project

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

    another c project

    hi all, gotta solve a problem: this is a project called post_office, his aim is to simulate insertion and shipment of postal products from different cities (cases a and b) and show information about cities and products in their shipping list (case c)

    the problem is that in case b whatever string I put it doesn't ship anything.

    it seems there's a problem with the for cycle (line 166), but I really don't catch it.

    I hope someone here could help me, this is the code

    C pastebin - collaborative debugging tool

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Have you done any debugging with your IDE's tools?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #typedef struct a_city{
    #        int cod_num;
    #        char name[15];
    #        char pos[7];
    #        char cap[6];
    #    product listprod[MAX_PRODOTTI];
    #        int num_prod;
    #}city;
    #
    #//dati delle citta'
    #char city_data [CITTA*3][15]={  "Avellino","Sud","83100",
    #                            "Benevento","Sud","82100",
    #                            "Firenze","Centro","50100",
    #                            "Genova","Nord","16100",
    #                            "Milano","Nord","20100",
    #                            "Napoli","Sud","80100",
    #                            "Perugia","Centro","06100",
    #                            "Roma","Centro","00100",
    #                            "Torino","Nord","10100",
    #                            "Venezia","Nord","30100"};
    This would be better if you made 'city' its own structure, and then made another one that had a city + its product.
    Code:
    typedef struct a_city{
            int cod_num; /* remove this if it's not actually something relevant, and stick it in the other structure */
            char name[15];
            char pos[7];
            char cap[6];
    } cityinfo;
    
    typedef struct productioninfo {
        cityinfo thiscity;
        product listprod[MAX_PRODOTTI];
        int num_prod;
    } cityproduction;
    That way, the whole problem you are having with swapping citys around would be much easier to fix. Plus, your city list would instead look like:
    Code:
    cityinfo cities[] = {
        { /*whatever that number is... or remove it */ 5, "Benevento","Sud","82100", },
        ...the rest of the cities...
    };

    Quzah.
    Last edited by quzah; 07-08-2009 at 12:44 AM. Reason: color
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    16
    Quote Originally Posted by whiteflags View Post
    Have you done any debugging with your IDE's tools?
    no I didn't... I use vi+gcc but I can also use CodeBlocks, what should I do to debug?

    do you mean to use assert o what?

    sorry for my lack of experience

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    16
    the problem presents itself only when we have to ship the LAST product in the shipping cue; so it seems to be related to the fact that when I put
    strcpy(city[i-1].listprod[j].type,city[i-1].listprod[j+1].type);
    it finds nothing on j+1; should I inizialize all city[i].listprod[j].type to NULL to solve it?

    edit it doesn't work... please help me
    Last edited by thenewbiecoder; 07-08-2009 at 01:38 AM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by thenewbiecoder View Post
    no I didn't... I use vi+gcc but I can also use CodeBlocks, what should I do to debug?

    do you mean to use assert o what?

    sorry for my lack of experience
    Learn about breakpoints and watching variables at least under CodeBlocks.... being able to watch your own programs execute under test data is the key to ensuring that they work correctly. It also helps you ask more competent questions here after you're really stuck. A lot of the time, just watching code execute, you can fix your own problems.

    the problem presents itself only when there's one single product in shipping list; so it seems to be related to the fact that when I put
    strcpy(city[i-1].listprod[j].type,city[i-1].listprod[j+1].type);
    it finds nothing on j+1; should I inizialize all city[i].listprod[j].type to NULL to solve it?
    Dereferencing NULL will cause undefined behavior in your program, so your suggestion is not safe.

    I think you need to restructure some of the data you use, like quzah said, to fix this appropriately. If you're having trouble staying within the bounds of your arrays something is far too complicated.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should be initializing your product list to have nothing in it before you start using. You're still over complicating this with all the string swapping and such. Why don't you just have a product list and store a list of products by number, rather than swapping string all around? Same thing with cities.
    Code:
    struct productinfo {
        char *productname;
        int productvalue;
        int whatevercrapyoufeellikeputtingin;
    };
    
    struct {
        int product;
        int quantity;
    } productinfo;
    
    struct cityproduction {
        int citynumber;
        struct productinfo productlist[ MAXPRODUCTS ];
    };
    Now you'd pretty much have to rewrite the whole thing here, but it would be a lot cleaner. Build a list of all your products. Build a list of all your cities. Reference cities by their number in their table, and products the same way. There are a lot of variations on the theme you could do here. But I'm not sure that you need to keep swapping strings all over the place, when numbers are much easier to swap and deal with.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    16
    solved it, thanks anyway to all of you that gave me suggestions but I really didn't ask how to optimize the code, I just had a problem with a code that I needed to complete in two hours

    the solution was a simple if ^_^
    Code:
    for(j=0;j<city[i-1].num_prod;j++){
                                    if((j==city[i-1].num_prod-1) && (strcmp(prodotto,city[i-1].listprod[j].type)==0)){
                                            city[i-1].num_prod=city[i-1].num_prod-1; //decrementa il totale dei prodotti in spedizione
                                            printf("\nProdotto spedito con successo!\n\n");
                                    }
                                    else if((j!=city[i-1].num_prod-1) && (strcmp(prodotto,city[i-1].listprod[j].type)==0)){
                                        for(;j<city[i-1].num_prod-1;j++){ //non inizializza j, utilizza il valore incrementato dall'altro ciclo
                                            city[i-1].listprod[j].code_n=city[i-1].listprod[j+1].code_n;
                                            strcpy(city[i-1].listprod[j].type,city[i-1].listprod[j+1].type);
                                            city[i-1].listprod[j].prezzo=city[i-1].listprod[j+1].prezzo;
                                            city[i-1].num_prod=city[i-1].num_prod-1; //decrementa il totale dei prodotti in spedizione
                                            printf("\nProdotto spedito con successo!\n\n");
                                        }
                                    }
                                    else if((j==city[i-1].num_prod-1) && (strcmp(prodotto,city[i-1].listprod[j].type)!=0)){
                                        printf("\nProdotto non trovato.\n\n");
                                    }
                                }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM