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.