| | |
passing structuce to function as an argument
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 28
Reputation:
Solved Threads: 0
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
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!
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
C Syntax (Toggle Plain Text)
#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; }
Please help me find the mistake!
Thanks a lot!
3
#2 32 Days Ago
>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.
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.
I'm here to prove you wrong.
![]() |
Similar Threads
- Passing command line argument to function (C++)
- tkinter button widget - passing parameter to function (Python)
- Does it need a copy constructor and an operator= for a class which have array member (C++)
- Passing a member function pointer (C++)
- Urgent Help in Binary Search Trees.. (C++)
- passing `const' as `this' argument of `' discards qualifiers (C++)
- Pointer to function as an argument (C++)
Other Threads in the C Forum
- Previous Thread: switch case problem with characters
- Next Thread: raw sockets using mac addresses
| Thread Tools | Search this Thread |
#include adobe ansi api append array arrays asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework frequency function getlasterror givemetehcodez global grade gtkgcurlcompiling hacking hardware highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab windows.h windowsapi






