Thread: Structure Functions

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    Structure Functions

    I am writing a function that compares two structures. It compares the names and the quantity. The total cost is then the price times the quantity wanted. if the the item is not there then there is a printed error and if the quantity exceeds the available quanitity then prints an error message. if the the item name is there and the item quantity is available, then it prints the item, the quanitity, and the price. My outoput looks like this
    There are 4 items in inventory.
    There are 3 items in grocery list
    Item: potatoes
    does not exist
    Item: 2-liter soda
    does not exist
    Item: pounds of hamburger does not exist
    Item: done
    does not exist
    ------------------------------
    Total Cost: $0.00
    This is my code:
    Code:
    typedef struct{   
        char name[STRINGMAX];
        int quantity; 
    }Grocery_List_Item; 
    
    typedef struct{
          char name[STRINGMAX];
          int quantity;
          float price;
    } InvItem;
    
    typedef InvItem InvArr[ITEMMAX];    
    typedef Grocery_List_Item Grocery_Arr[ITEMMAX];   
    
    int availability(Grocery_Arr groc_list, InvArr inv_list); 
    int fill_grocery_list(Grocery_Arr groc_list); 
    int fill_inv(InvArr inv_list); 
    void empty_str(char name[]); 
     
    int main(){ 
      InvArr inv_item; 
      Grocery_Arr want_item; 
      int num_InvItems, num_GrocItems; 
     
      num_InvItems = fill_inv(inv_item); 
      num_GrocItems = fill_grocery_list(want_item); 
      printf("There are %d items in inventory.\n",num_InvItems); 
      printf("There are %d items in the grocery list.\n",num_GrocItems); 
      printf("Purchasing:\n"); 
      availability(want_item, inv_item); 
     
      return 0; 
    } 
     
     
    void empty_str(char name[]){ 
      char *last = strrchr(name,'\n'); 
      if(last != NULL) 
        *last = '\0'; 
    }
    
    int fill_inv(InvArr inv_list){ 
      int count = 0; 
      char temparr[STRINGMAX + 1]; 
      do{ 
        scanf("%f", &inv_list[count].price); 
        scanf("%d", &inv_list[count].quantity); 
        fgets(temparr, STRINGMAX, stdin); 
        strcpy(inv_list[count].name, temparr); 
        empty_str(temparr); 
      }while(inv_list[count++].price !=0); 
     
      return (count - 2); 
    } 
     
    int fill_grocery_list(Grocery_Arr groc_list){ 
      int count = 0; 
      char temparr[STRINGMAX + 1]; 
      do{ 
        scanf("%d",&groc_list[count].quantity); 
        fgets(temparr, STRINGMAX + 1, stdin); 
        strcpy(groc_list[count].name, temparr); 
        empty_str(temparr); 
      }while(groc_list[count++].quantity !=0); 
     
      return (count - 1); 
    }
    
    int availability(Grocery_Arr groc_list, InvArr inv_list){ 
      int match, count = 0; 
      float total = 0.0, new_price = 0; 
      do{ 
        match = strcmp(groc_list[count].name, inv_list[count].name); 
        if((match == 0) && (groc_list[count].quantity <= inv_list[count].quantity)){ 
          new_price += (groc_list[count].quantity * inv_list[count].price); 
          printf("%s %d $%f\n",groc_list[count].name,groc_list[count].quantity,new_price); 
          total += new_price; 
        } 
        else if((match == 0) && (groc_list[count].quantity > inv_list[count].quantity)) 
          printf("Item: %s insufficient quantity\n",groc_list[count].name); 
        else 
          printf("Item: %s does not exist\n",groc_list[count].name); 
      }while(groc_list[count++].quantity !=0); 
      printf("-------------------------\n"); 
      printf("Total Cost: $ %.2f\n",total); 
      return total; 
    }
    Last edited by trprince; 12-12-2007 at 08:00 PM. Reason: added code

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    You forgot to add your question.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    oksy so now I have two problems. The last sentinel (done 0) is printing and i don't want that because its my sentinel. And also the big problem is that my numbers new_price is multiplying wrong. I know it's not multiplying by the correct price that matches the name of the item. Can you help me fix that? Please

    output:
    There are 4 items in inventory.
    There are 3 items in the grocery list.
    Purchasing:
    potatoes 5 $12.45
    2-liter soda 3 $0.93
    pounds of hamburger 2 $2.50
    done 0 $0.00
    -------------------------
    Total Cost: $ 15.88
    Code:
    int fill_inv(InvArr inv_list){ 
      int count = 0; 
      char temparr[STRINGMAX+2];
      do{ 
        scanf("%f %d", &inv_list[count].price, &inv_list[count].quantity);
        fgets(temparr, STRINGMAX+2, stdin);
        strncpy(inv_list[count].name, temparr, STRINGMAX);
        inv_list[count].name[STRINGMAX+1]='\0';
        empty_str(temparr); 
      }while(inv_list[count++].price !=0); 
     
      return (count - 1); 
    } 
    
    int fill_grocery_list(Grocery_Arr groc_list){
      int count = 0;
      char temparr[STRINGMAX+2];
      do{
        scanf("%d",&groc_list[count].quantity);
        fgets(temparr, STRINGMAX+2, stdin);
        empty_str(temparr);
        strncpy(groc_list[count].name, temparr, STRINGMAX);
        groc_list[count].name[STRINGMAX+1] = '\0';
      }while(groc_list[count++].quantity != 0);
     
      return (count - 1); 
    } 
     
     
    int availability(Grocery_Arr groc_list, InvArr inv_list){ 
      int match, count = 0; 
      float total = 0.0, new_price; 
      do{ 
        match = strcmp(groc_list[count].name, inv_list[count].name); 
        if((match) && (groc_list[count].quantity <= inv_list[count].quantity)){ 
          new_price = ((float)groc_list[count].quantity * inv_list[count].price); 
          printf("%s %d $%.2f\n", groc_list[count].name, groc_list[count].quantity, new_price); 
          total += new_price; 
        } 
        else if((match) && (groc_list[count].quantity > inv_list[count].quantity)) 
          printf("Item: %s insufficient quantity\n",groc_list[count].name); 
        else 
          printf("Item: %s does not exist\n",groc_list[count].name); 
      }while(groc_list[count++].quantity !=0); 
      printf("-------------------------\n"); 
      printf("Total Cost: $ %.2f\n",total); 
      return total; 
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    1. The last sentinel (done 0) is printing and i don't want that because its my sentinel
    I see 2 possibilitie
    - do not add this item to the list (requires more changes
    - do not print it - small fix needed:
    in the function availability replace the do/while(); loop with the while loop keeping the condition

    2. Why do you think that new price is multiplied wrong? Provide the input data, expected result for 1 Item and received result
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    So just make it a plain while loop?

    It's multiplying by the first one then the second one and so on.


    ::::::::::::::
    in1
    ::::::::::::::
    2.49 9 pounds of hamburger
    .31 20 potatoes
    1.25 12 vitamin water
    .89 8 2-liter soda
    0 0 done
    5 potatoes
    3 2-liter soda
    2 pounds of hamburger
    0 done
    ::::::::::::::
    out1
    ::::::::::::::
    There are 4 items in inventory.
    Purchasing:
    potatoes 5 $1.55
    2-liter soda 3 $2.67
    pounds of hamburger 2 $4.98
    ---------------------
    Total Cost: $9.20

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    match = strcmp - strcmp returns 0 for matched strings - check your condition on match
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    what do you mean check the condition. If it does =0 then do the following statements. Should I chagne it?

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    if((match) - it is true when string do not match.

    Change the variable name or how you assign a value, so that the value will be consistent with the name
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    even when i change the name, I'm still gettting the same thing. I don't really understand.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    You assign match to be the result of a strcmp. But if the strings match, strcmp returns 0 -- which means if (match) does the opposite of what you think/want it to do (since match, interpreted as true/false, will give false when they match and true when they don't).

    So the suggestion was to change the variable name to something like not_match, then in your if you can do if (!(not_match) && ....)

    EDIT: Or, you had it right the first time, looking back. If (match==0 ...)
    Last edited by tabstop; 12-13-2007 at 11:38 AM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Better yet, define it something like a bool and so
    Code:
    match = (strcmp(groc_list[count].name, inv_list[count].name) == 0);
    Will save true if strcmp returned 0 (which it does if strings match) or false if they did not match (strcmp returned something other than 0).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    ok...so when i do the

    Code:
    match = (strcmp(groc_list[count].name, inv_list[count].name) == 0);
    i get this output

    There are 4 items in inventory.
    There are 3 items in the grocery list.
    Purchasing:
    Item: potatoes does not exist
    Item: 2-liter soda does not exist
    Item: pounds of hamburger^B does not exist
    Item: done does not exist
    -------------------------
    Total Cost: $ 0.00

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    it doesn't even read them...i'm so out of ideas

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    ok so now my functon is this

    Code:
    int availability(Grocery_Arr groc_list, InvArr inv_list, int num_items){ 
       int count;  
       float total = 0.0, new_price;  
       for(count = 0; count < num_items; count++){   
         if((strcmp(groc_list[count].name, inv_list[count].name)) != 0){  /*COMPARE THE NAMES OF THE ITEMS*/ 
            if(groc_list[count].quantity <= inv_list[count].quantity){   /*IF AVAILABLE FOR PURCHASE*/ 
               new_price = ((float)groc_list[count].quantity * inv_list[count].price);   /*CALCULATE NEW PRICE*/
               printf("%s %d $%.2f\n", groc_list[count].name, groc_list[count].quantity, new_price);
               total += new_price;   /*CALCULATE NEW TOTAL*/ 
            }
            else if(groc_list[count].quantity > inv_list[count].quantity)  /*IF AVAILABLE BUT NOT RIGHT QUANTITY*/ 
               printf("Item: %s insufficient quantity\n",groc_list[count].name);  
         }     
    else  
        printf("Item: %s does not exist\n",groc_list[count].name);  /*IF ITEM DOES NOT EXIST*/
       } 
       printf("-------------------------\n");
       printf("Total Cost: $ %.2f\n",total);   
       return total; 
    }
    with an output of

    There are 4 items in inventory.
    There are 3 items in the grocery list.
    Purchasing:
    potatoes 5 $12.45
    2-liter soda 3 $0.93
    pounds of hamburger 2 $2.50
    -------------------------
    Total Cost: $ 15.88

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    You're using the same index for the groc_list and the inv_list, which is why the first item bought always costs $2.49, the second costs $0.31, the third costs $1.25, etc.

    You need to loop through the grocery list, and inside that loop loop through the inventory. You'll need max_items for both lists, though, it looks like.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Functions in a Class Structure?
    By AndyBomstad in forum C++ Programming
    Replies: 3
    Last Post: 06-15-2005, 04:51 AM
  3. Structure Problem
    By Generator in forum C Programming
    Replies: 5
    Last Post: 07-28-2003, 11:54 PM
  4. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM
  5. structure arrays to functions
    By caws in forum C Programming
    Replies: 9
    Last Post: 04-13-2003, 03:37 PM