1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

inventory program

Discussion in 'C' started by nylrig, Mar 21, 2011.

  1. nylrig

    nylrig New Member

    Joined:
    Mar 21, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hey guys i need your help. please help me check whats wrong with my program.. i need to correct it as soon as possible. this is one of our requirements in our class... well, actually tomorrow is the deadline of this... guys help me please... thanks:)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    
    struct node_item {
        struct node_item *next;
        int id;
        char name[128];
        float price;
    };
    
    struct node_r {
        struct node_r *next;
        int id;
        int quantity;
    };
    
    void welcome();
    void add();
    void del();
    void cashreg();
    void buy();
    void pay();
    void insert(struct node_item*p, int id, float price, char *name);
    void rem(struct node_item *p, int id);
    void r_insert(struct node_r *p1, int id, int quantity);
    void save(struct node_item *p);
    void list_ini(struct node_r *p1);
    void list_init(struct node_item *p);
    
    int main () {
        FILE *fp = fopen("database.txt", "r");
        int id;
        char name[128];
        float price;
        struct node_item p;
        [URL=http://www.go4expert.com/articles/c-cpp-assert-function-t27488/]assert[/URL] (fp);
        while (1) {
            fscanf(fp, "%d %s %f", id, name, price);
            if (feof(fp) || ferror(fp)) break;
            insert(&p, id, price, name);
        }
        fclose(fp);
        welcome();
        fp = fopen ("database.txt", "w");
        save(&p);
        fclose(fp);
        return 0;
    }
    
    void welcome() {
        int go;
        printf("1. Add item to inventory\n");
        printf("2. Delete item from inventory\n");
        printf("3. Cash register\n");
        printf("4. Exit\n");
        scanf("%d", &go);
    
        if (go == 1) add();
        else if (go == 2) del();
        else if (go == 3) cashreg();
    }
    
    void add() {
        int id, quantity;
        char name[128];
        double price;
        struct node_item p;
        FILE *fp;
    
        printf("Unique id: ");
        scanf("%d", &id);
        printf("Name: ");
        scanf("%s", name);
        printf("Price: ");
        scanf("%lf", &price);
    
        insert(&p, id, price, name);
    }
    
    void del() {
        int id;
        struct node_item p;
        FILE *fp;
        printf("Unique id: ");
        scanf("%d", &id);
        rem(&p, id);
    }
    
    void cashreg() {
        int to;
    
        printf("1. Buy an item\n");
        printf("2. Pay\n");
        printf("3. Go back to Inventory screen\n");
        scanf("%d", &to);
    
        if (to == 1) buy();
        else if (to == 2) pay();
        else welcome();
    }
    
    
    void buy() {
        int id, quantity;
        struct node_r p1;
        FILE *f = fopen("receipts.txt", "w");
        list_ini(&p1);
        printf("Unique id: ");
        scanf("%d", &id);
        printf("Quantity: ");
        scanf("%d", &quantity);
    
        r_insert(&p1, id, quantity);
        fclose(f);
        cashreg();
    }
    
    void insert(struct node_item *p, int id, float price, char *name) {
    	while (p->next != 0) p = p->next;
    	p->next = (struct node_item*) malloc(sizeof(struct node_item));
    	p->next->id = id;
    	p->next->price = price;
    	strcpy(p->next->name, name);
    	p->next->next = 0;
    }
    
    void rem(struct node_item *p, int id) {
    	while (p->next != 0 && p->next->id != id) p = p->next;
    	if (p->next == 0) {
    		printf("list_remove: could not remove %d\n", id);
    		return;
    	}
    	else {
    	struct node_item *tmp = p->next->next;
    	free(p->next);
    	p->next = tmp;
    	}
    }
    void r_insert(struct node_r *p1, int id, int quantity) {
    	while (p1->next != 0) p1 = p1->next;
    	p1->next = (struct node_r *) malloc(sizeof(struct node_r));
    	if (p1->next->id = id) p1->next->quantity += quantity;
        else {
            p1->next->id = id;
            p1->next->quantity = quantity;
        }
    	p1->next->next = 0;
    }
    void save(struct node_item *p) {
        FILE *fp;
        while (p->next != 0) p = p->next;
        fprintf(fp, "%d %s %f", p->next->id, p->next->name, p->next->price);
    }
    void list_init(struct node_item *p) {
    	p->id = -1;
    	p->next = 0;
    }
    void list_ini(struct node_r *p1) {
    	p1->id = -1;
    	p1->next = 0;
    }
    void pay() {
        struct node_r p1;
        FILE * f = fopen("receipts.txt", "r");
        int id, quantity;
        double price;
        assert(f);
        while(1) {
            fscanf(f, "%d %d", &id, &quantity);
            if (feof(f) || ferror(f)) break;
            printf("%d %d", id, quantity);
        }
    }
     

Share This Page