| | |
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 Nov 4th, 2009
>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 |
adobe ansi api array arrays bash binarysearch centimeter char character convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush fgets file floatingpointvalidation fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest intmain() kilometer license linked linkedlist linux linuxsegmentationfault list logical_drives match matrix meter microsoft motherboard multi mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings structures suggestions test testautomation unix urboc user voidmain() win32api windows.h






