Thread: if/else within a struct.

  1. #1
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22

    if/else within a struct.

    Created a struct...

    Code:
    typedef struct {
    	char name[40];
    	short int number;
    	double wholesale;
    	double retail;
    	short int onHand;
    } PRODUCT;
    Created a list of items:
    Code:
    	strcpy(inventory[*pSize].name, "Hammer");
    	inventory[*pSize].number = 3457;
    	inventory[*pSize].onHand = 4;
    	inventory[*pSize].wholesale = 10.00;
    	inventory[*pSize].retail = 14.28;
    	(*pSize)++;
    I need a snippet of C code that will run through the list of items within the struct and list out inventory items that are less than 5 onHand.

    code would say onHand <= 5 then printf. but not sure what that will look like using a struct. Can someone help me out with this? Thanks in advance.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well you will have show us your attempt on this, its clearly not a rocket science. Here is an hint

    Code:
    while( *pSize < MaxSize )
    {
        if( inventory[*pSize].onHand <= 4 )
             print all the invent information 
        (*pSize)++;
    }
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22
    Quote Originally Posted by ssharish2005 View Post
    Well you will have show us your attempt on this, its clearly not a rocket science. Here is an hint

    Code:
    while( *pSize < MaxSize )
    {
        if( inventory[*pSize].onHand <= 4 )
             print all the invent information 
        (*pSize)++;
    }
    ssharish
    Perfect Harish that's exactly what I was looking for. I think it was the early hour and my brain wheels were not turning yet. Thank you so much.

    Code:
    void inventoryReport(PRODUCT inventory[], int *pSize){
    	int i = 0;
    
    	printf("   Inventory Report   \n");
    	printf("   ****************   \n");
    	printf("Name    Product Number   On Hand   Wholesale Price   Retail Price \n");
    	printf("****    **************   *******   ***************   ************ \n");
    
    	for(i = 0; i < *pSize; i++ ){
    		printf("%10s%10i%10i%10.2lf%10.2lf\n", inventory[i].name, inventory[i].number,
    			inventory[i].onHand, inventory[i].wholesale, inventory[i].retail);
    	}
    
    }
    
    void lowInventoryReport(PRODUCT inventory[], int *pSize) {
    
    }
    main (){
    	int effectiveSize = 0;
    
    	PRODUCT hammer = {"Hammer", 1234, 5.50, 8.99, 4};
    	PRODUCT inventory[500];
    
    	fillInProducts(inventory, &effectiveSize);
    	inventoryReport(inventory, &effectiveSize);
        
      system("Pause");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting struct to struct
    By klmdb in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 02:29 PM
  2. I'm donating my vector lib.
    By User Name: in forum C Programming
    Replies: 23
    Last Post: 06-24-2010, 06:10 PM
  3. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  4. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  5. Replies: 10
    Last Post: 05-18-2006, 11:23 PM