944,105 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 441
  • C RSS
Nov 4th, 2009
0

passing structuce to function as an argument

Expand Post »
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
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #define MAX 50
  4.  
  5. struct good
  6. {
  7. int id;
  8. int type;
  9. float price;
  10. };
  11. struct good item[MAX];
  12. int n;
  13. int pos;
  14.  
  15. void input()
  16. { char temp[40];
  17. int i;
  18. printf("how many items u want to store: "); scanf("%d",&n);
  19. for(i=0; i<n; i++)
  20. {
  21. printf("\nItem %d: ",i+1);
  22. do
  23. {
  24. printf("\n id:");
  25. scanf("%d",&item[i].id);
  26. } while( (item[i].id<0) || (item[i].id>9999) );
  27. printf("\n Type: ");
  28. fflush(stdin);
  29. gets(temp);
  30. if(strcmp(temp,"food")==0)
  31. item[i].type=1;
  32. else if(strcmp(temp,"electrical")==0)
  33. item[i].type=2;
  34. else if(strcmp(temp,"household")==0)
  35. item[i].type=3;
  36. printf("\n Price: ");
  37. scanf("%f",&item[i].price);
  38. }
  39. }
  40.  
  41. void display()
  42. {
  43. int i;
  44. for(i=0; i<n; i++)
  45. {
  46. printf("Item %d: ",i+1);
  47. printf("\n id %d, type %d, price %f\n",item[i].id,item[i].type, item[i].price);
  48. }
  49. }
  50.  
  51.  
  52.  
  53. struct good price(struct good arr[])
  54. {
  55. int i,pos;
  56. struct good temp;
  57.  
  58. temp = item[0];
  59. for(i=0; i<n; i++)
  60. {
  61. if(temp.price < item[i].price)
  62. temp = item[i];
  63. }
  64. return temp ;
  65. }
  66.  
  67. void display_item(struct good *p)
  68. {
  69. printf("\n id %d, type %d, price %f\n",p -> id, p -> type, p -> price);
  70. }
  71.  
  72. int main()
  73. {
  74. input();
  75. display();
  76. struct good item_max;
  77. item_max =struct good price(item[MAX]);
  78. display_item(&item_max);
  79. getch();
  80. return 0;
  81. }
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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
thebluestar is offline Offline
55 posts
since Apr 2009
Nov 4th, 2009
3
Re: passing structuce to function as an argument
>Please help me find the mistake!
I suspect the mistake is a complete failure to study whatever book on C you have. Instead of trying to finish this exercise in one swell foop, perhaps you should be writing a simple skeleton that passes structures around. At least that way you'll fail on fundamental concepts much sooner and will have an easier time sorting things out.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: switch case problem with characters
Next Thread in C Forum Timeline: raw sockets using mac addresses





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC