Thread: Problem with Program

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    6

    Problem with Program

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    FILE* fin;
    FILE* fout;
    
    
    
    typedef struct KnightsMartProduct {
            int itemNum;
            char itemName[21];
            double unitPrice;
            int stockQty;
            int restockQty;
            struct KnightsMartProduct *next;
    } KMProduct;
    
    typedef struct KnightsMartRestockProduct {
            int itemNum;
            struct KnightsMartRestockProduct *next;
    } KMRestockProduct;
    
    typedef struct KnightsMartSale {
            char firstName[21];
            char lastName[21];
            int numItemsonList;  // # of items on shopping list
            int *itemsPurchased; // array of item numbers
            struct KnightsMartSale *next;
    } KMSale;
    
    //KMProduct ordered linked list of products
    struct KnightsMartProduct* insertAdd(struct KnightsMartProduct *Products, FILE* fin,int NewItemNum,char NewItemName[],int itemPrice,int stockQty,int restockQty);
     struct KnightsMartRestockProduct* delete(struct KnightsMartProduct *Products,struct KnightsMartRestockProduct *RestockList, FILE* fin, int ItemNum);
    struct KnightsMartProduct* insertAdd(struct KnightsMartProduct *Products, FILE* fin,int NewItemNum,char NewItemName[],int itemPrice,int stockQty,int restockQty)
    {
    
    KMProduct *pNew = (KMProduct *) malloc(sizeof(KMProduct));
    KMProduct *help_ptr = Products;
    pNew->itemNum = NewItemNum;
    //pNew->itemName = NewItemName;
    pNew->unitPrice = itemPrice;
    pNew->stockQty = stockQty;
    pNew->restockQty = restockQty;
    fscanf (" %d ", &pNew->itemNum);
           if ( help_ptr == NULL || help_ptr->itemNum > NewItemNum) {
                    pNew->next = Products;
                    Products = pNew;
                    return Products;
                    }      
                    
           while(help_ptr->next != NULL && help_ptr->next->itemNum < NewItemNum)
                   help_ptr = help_ptr->next;
           
           pNew->next = help_ptr->next;
           help_ptr->next = pNew;
           return Products;
           }    
           
           //printf("\n");
           
           
           
    
    struct KnightsMartRestockProduct* delete(struct KnightsMartProduct *Products,struct KnightsMartRestockProduct* RestockList, FILE* fin,int ItemNum){
           struct KnightsMartRestockProduct *help_ptr, *node2delete;
           help_ptr = RestockList;
           if (help_ptr != NULL){
                        if (help_ptr->itemNum == ItemNum  ){
                                              
                                              RestockList = help_ptr->next;
                                              free(help_ptr);
                                              return RestockList;
                        }
                        while (help_ptr->next != NULL  ){ 
                              
                              if (help_ptr->next->itemNum == ItemNum){
                                                          
                                                          node2delete = help_ptr->next;
                                                          help_ptr->next = help_ptr->next->next;
                                                          free(node2delete);
                                                          return RestockList;
                              }
                              help_ptr = help_ptr->next;
                        }
           }
           return RestockList;
    }
    
    struct KnightsMartSale* Sale(struct KnightsMartSale *cSale){//, FILE * fin){//, char firstName, char lastName, int numItemsonList
    
    KMSale *pNew = (KMSale *) malloc(sizeof(KMSale));
    &pNew->itemNum = itemNum;
    KMSale *help_ptr = cSale;
    fscanf(fin, "%s %s %d", &pNew->firstName, &pNew->lastName, &pNew->itemNum);
    
    
                            }
                                                      
    
    //KMSales 
     
    //Printf ("%d %s %lf);
    
    int main(int argc, char *argv[])
    {
        KMProduct *Products = NULL;
        KMRestockProduct *RestockList = NULL;
        KMSale *cSale = NULL;
        int NewItemNum;
        char NewItemName[21];
        int itemPrice;
        int stockQty;
        int restockQty;
        
        char command[20];
        int itemNum;
        FILE *fin;
        fin =  fopen("KnightsMart.txt", "r");
        
        fscanf (fin, "%s", &command);
        delete(Products,RestockList,fin,itemNum);
        if(strcmp(command, "ADDITEM")== 0){
                           fscanf(fin, " %d %s %lf %d %d", &Products->itemNum, &Products->itemName, &Products->unitPrice, &Products->stockQty, &Products->restockQty);
                           Products = insertAdd (Products, fin, NewItemNum, NewItemName, itemPrice, stockQty, restockQty);        
                           //fscanf(fin, "%s %s %d", &pNew->firstName, &pNew->lastName, &pNew->itemNum);
                           
                           //KMSale* pnew(KMSale *)malloc(sizeof(KMSale)) ;
                           //pNew-> =
                          Sale(cSale);
                          
        //if(strcmp(command, "RESTOCK")== 0){
                          // fscanf(fin, " %d %d %d", &Products->itemNum, &Products->stockQty, &Products->restockQty);
                           
        
         //if(strcmp(command, "CUSTOMER")== 0){    
                              
                                          
    }
    
    
    
    //printf ("\t| Item %6d | %-20s | 
        
      system("PAUSE");	
      return 0;
    }
    Attached Files Attached Files

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Were you going to mention a problem? Your add looks broken, unless you are using the return value for something other than what I'm expecting you to use the return value for.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    In the gist of my situation I am trying to make linked list for additem, restocklist, and sales. I need to read in a file that gets information by line and then returns back the daily sales, and also when the stock qty is 0 then it calls restock and increases stock qty. I am completely lost I am having problem how to print out information also how to make the linked list for inventory.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Announcements - C Programming
    How To Ask Questions The Smart Way

    Ask a question. Does it even compile? If not, what errors do you get that you can't fix. If it compiles, tell us what is wrong. What output do you expect and what do you get?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When the stock qty is 0, then "it" calls restock. What is "it" -- the data file you are reading, or your program? If your program, then you need to check when you sell something what that does to the stock.

    What do you not like about making the linked list for inventory?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    This program needs to manage products in a store; along with stock for a product
    also manages reordering of depleted products once item reaches zero
    and then manage the total sales to all customers in a given day

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So once again, where's the question? What doesn't it do that it should, or what does it do that it shouldn't? Ask a smart question, and you'll have a better chance at actually getting a reply that you find useful.



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

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    Linked lists are confusing. I am confused I made the insertadd to add products to linked list. I am reading from a file and I am having a problem printing out the product node of the list using fscanf. How do I print out the products to ensure its working?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't print things with fscanf. If you intend to print things to the screen, use printf. If you want to print the list, then you should be able to walk through the list from beginning to end printing as you go.

    If you're worried about reading things in (and who isn't?), then you should check the return value of fscanf.
    Code:
    num = fscanf(fin, " %d %s %lf %d %d", &Products->itemNum, &Products->itemName, &Products->unitPrice, &Products->stockQty, &Products->restockQty);
    if (num != 5) {
        /* Not everything was read!  Panic! */
        fprintf(stderr, "Only %d things read!\n", num);
    }
    Also, %s will not read strings with spaces in them, if that is required. Also, don't use & in front of array names, like Products->itemName.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make a function called "printproduct" that takes a "product" and prints every value of that product. If your "product" is a structure, simply print the members of that structure in some nice format. Now call "printproduct" in a loop as you walk through your list.

    Actually stopping and thinking about what you need to do really does go a long way.


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

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    Okay. When I use fscanf (" %d ", &pNew->itemNum);
    and I only want itemNum to print out. How do I do this?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are doing linked lists and you haven't covered using printf? For that matter, you are using *scanf and you haven't covered *printf yet? Read the fine man page.


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

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    I got printf ( "d" , itemNum);
    my problem when using fscanf the program says itemNum undeclared?

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    It would be really helpful for us AND you to post current code, with decent indentation and whitespace. As it stands now, it nearly impossible to notice where one function ends and the next begins.

    my problem when using fscanf the program says itemNum undeclared?
    I don't think your program says it's undeclared, your compiler maybe... If you posted updated code and the exact compiler error, it'd be easier to track down. However, it looks like you are trying to use itemNum (which, for the most part, is an element of a structure) by itself which you cannot do. (To confuse us more, you have itemNum declared in main(), but I suspect that's NOT where the compiler is complaining)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in a C program
    By matt345 in forum C Programming
    Replies: 6
    Last Post: 01-08-2011, 08:56 AM
  2. Problem with my program...
    By saya_makan36 in forum C Programming
    Replies: 1
    Last Post: 03-09-2010, 06:49 AM
  3. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  4. A problem with my program.
    By eXeCuTeR in forum C Programming
    Replies: 14
    Last Post: 11-22-2007, 02:56 PM
  5. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM

Tags for this Thread