This is the problem:
In this exercise you have to write a program to manage items in a super market.
Suppose that the data about each item includes:
• Item’s ID: ID of the item, it is an integer which ranges from 1 to 9999
• Item’s type: Its value is:
• 1 if the item is food
• 2 if the item is electrical good
• 3 if the item is household good
• Item’s price
1. Create a structure template that contains data of one item. Then use this template to declare an array
of structures that stores data of maximum 50 items.
2. Ask user to input the list of N items from the keyboard. Then print out the list of items on the screen.
3. Print out the title of the item which has maximum price.
Here is my source code
#include<stdio.h>
#include<conio.h>
#define MAX 50
struct good
{
int id;
int type;
float price;
};
struct good item[MAX];
int n;
int pos;
void input()
{ char temp[40];
int i;
printf("how many items u want to store: "); scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\nItem %d: ",i+1);
do
{
printf("\n id:");
scanf("%d",&item[i].id);
} while( (item[i].id<0) || (item[i].id>9999) );
printf("\n Type: ");
fflush(stdin);
gets(temp);
if(strcmp(temp,"food")==0)
item[i].type=1;
else if(strcmp(temp,"electrical")==0)
item[i].type=2;
else if(strcmp(temp,"household")==0)
item[i].type=3;
printf("\n Price: ");
scanf("%f",&item[i].price);
}
}
void display()
{
int i;
for(i=0; i<n; i++)
{
printf("Item %d: ",i+1);
printf("\n id %d, type %d, price %f\n",item[i].id,item[i].type, item[i].price);
}
}
struct good price(struct good arr[])
{
int i,pos;
struct good temp;
temp = item[0];
for(i=0; i<n; i++)
{
if(temp.price < item[i].price)
temp = item[i];
}
return temp ;
}
void display_item(struct good *p)
{
printf("\n id %d, type %d, price %f\n",p -> id, p -> type, p -> price);
}
int main()
{
input();
display();
struct good item_max;
item_max =struct good price(item[MAX]);
display_item(&item_max);
getch();
return 0;
}
to solve problem number 3, I use a function to find the item having max price , then print its details ,but I cannot passing the structure "item" found in function struct good price(struct good arr[]) to display_item function.
Please help me find the mistake!
Thanks a lot!