Thread: Structure Functions

  1. #16
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    so know this is what i get
    There are 4 items in inventory.
    There are 3 items in the grocery list.
    Purchasing:
    Item: potatoes insufficient quantity
    Item: 2-liter soda insufficient quantity
    pounds of hamburger 2 $4.98
    -------------------------
    Total Cost: $ 4.98

    with this code:
    Code:
    int availability(Grocery_Arr groc_list, InvArr inv_list, int num_grocItems, int num_invItems){                               
       int compare, groc_count, inv_count;                                                                                       
       float total = 0.0, new_price;                                                                                             
       for(groc_count = 0; groc_count < num_grocItems; groc_count++){                                                            
         for(inv_count = 0; inv_count < num_invItems; inv_count++){                                                              
           compare = strcmp(groc_list[groc_count].name, inv_list[inv_count].name);  /*COMPARE THE NAMES OF THE ITEMS*/           
           if(compare == 0)                                                                                                      
             break;                                                                                                              
         }                                                                                                                       
           if(groc_list[groc_count].quantity <= inv_list[inv_count].quantity){   /*IF AVAILABLE FOR PURCHASE*/                   
              new_price = ((float)groc_list[groc_count].quantity * inv_list[inv_count].price);   /*CALCULATE NEW PRICE*/         
              printf("&#37;s %d $%.2f\n", groc_list[groc_count].name, groc_list[groc_count].quantity, new_price);                    
              total += new_price;   /*CALCULATE NEW TOTAL*/                                                                      
           }                                                                                                                     
           else if(groc_list[groc_count].quantity > inv_list[inv_count].quantity)  /*IF AVAILABLE BUT NOT RIGHT QUANTITY*/       
              printf("Item: %s insufficient quantity\n",groc_list[groc_count].name);                                             
                                                                                                                                 
         else                                                                                                                    
          printf("Item: %s does not exist\n",groc_list[groc_count].name);  /*IF ITEM DOES NOT EXIST*/                            
       }                                                                                                                         
       printf("-------------------------\n");                                                                                    
       printf("Total Cost: $ %.2f\n",total);                                                                                     
       return total;                                                                                                             
    }
    Last edited by trprince; 12-13-2007 at 08:49 PM. Reason: help

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    The only thing I see wrong here is that you're not really checking for no-match. If there is no match, then your for loop will run to completion, leaving inv_count as an invalid array index. So before checking for quantity, you should check that compare is still 0 (if not, then no-match, else do the quantity check). You should also put more print statements in your insufficient quantity code to see what's going on.

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