Thread: Linear Search and Struct

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    Linear Search and Struct

    I'm looking for some help with a problem I'm having with an assignment. I need to do a linear search on a structure but I keep getting 'illegal structure operation' error when I try and run the program at this line

    if (ItemToFind == ItemNumber[nIndex])




    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #define ITEMS 3
    
    struct sales
    {
            long number ;
            char description [21];
            float price;
    
    };
    
    
    int FindItem(struct sales ItemNumber[], int NumberOfItems, int ItemToFind);
    
    
    int main(int argc, char* argv[])
    {
            struct sales food [ITEMS] = {
    
            {111,"Cat food",1.49f},
            {338,"Whole wheat bread",0.79f},
            {123,"Water",3.49f},
    
            };
    
    
            int ItemPos,NewItem,amount;
            static float total = 0;
    
    
    
            do{
            printf("\nWhat item would you like to purchase? ");
            scanf(" %d",&NewItem);
            if(NewItem>0)
            {
            printf("How many items ? ");
            scanf(" %d",&amount);
            }
            else;
    
            }
            while(NewItem > 0);
    
    
            ItemPos = FindItem(food, ITEMS, NewItem);
    
    
            total = total + amount * food[ItemPos].price;
            printf("%.2f",total);
    
            getch();
            return 0;
    }
    
    
    
    
    int FindItem(struct sales ItemNumber[], int NumberOfItems, int ItemToFind)
    {
            int nIndex;
    
            for (nIndex = 0; nIndex < NumberOfItems; nIndex++)
            {
                    if (ItemToFind == ItemNumber[nIndex])
                    {
                           return nIndex;
                    }
            }
            return -1;      /* Part Not Found */
    
    }
    Any help or direction is greatly appreciated.

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Well you have created a struct called food that contains three items. So what item of the struct array are you trying to dereference for ItemNumber[nIndex]? Try this

    Code:
       ItemNumber[nIndex].number;

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Also once you get past that road block, whats with the endless loop as it appears your user will be stuck in an endless do/while loop, unless I overlooked how you are decrementing NewItem?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'm going to guess what you really want is this:
    Code:
    if (ItemToFind == ItemNumber[nIndex].number)
    It looks to me tho like "NewItem" will have to be 0 when this gets called so it won't work anyway.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    Smile

    Figured it out, thanks for all you help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM