C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 13th, 2002, 04:19 PM
Neildadon Neildadon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Posts: 12 Neildadon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
passing structs to functions ?

hi im having a few problems with this program im making. Its a library program being made in unix g++. When i compile i get 2 errors

neil.cc:120: conversion from `cust *' to non-scalar type `cust' requested
neil.cc:123: conversion from `cust *' to non-scalar type `books' requested


im having problems passing my structs to my functions

e.g. when 1 is pressed in the menu the member_list function should run and display all memembers of the library database. Im reading this data from a text file.

can anyone help im really desperate ??

#include <iostream.h>
#include <stdlib.h>
#include <string>
#include <fstream.h>

const int maxmembers = 1000;
const int maxbooks = 2000;

struct cust
{
string surname;
string house_no;
string road;
string ID;
int books_borrowed;
};

struct books
{
string author;
string title;
string category;
string ISBN;
int loaned;
};

void member_list(cust c)
{
for(int i=0; i< maxbooks;i++)
{
cout<< "Surname\t\tHouse No\t\tRoad\t\tID" << endl;
cout << c.surname[i] << endl;
cout << c.house_no[i] << endl;
cout << c.road[i] << endl;
cout << c.ID[i] << endl;
}
}

void book_list(books b)
{
for(int i=0; i< maxbooks;i++)
{
cout<< "Author\t\tTitle\t\tCategory\t\tISBN" << endl;
cout << b.author[i] << endl;
cout << b.title[i] << endl;
cout << b.category[i] << endl;
cout << b.ISBN[i] << endl;
}
}

main()
{
char uTest[100];
int cRead = 0;
int nTest[100];

//variables for type people and books
books book[maxbooks];
cust user[maxmembers];

ifstream user_data;
user_data.open("userdata.txt",ios::in);

do
{
user_data.getline(uTest,100,';');
user[cRead].surname = uTest;
user_data.getline(uTest,100,';');
user[cRead].house_no = uTest;
user_data.getline(uTest,100,';');
user[cRead].road = uTest;
user_data.getline(uTest,100,'\n');
user[cRead].ID = uTest;

if (user_data)
{
cRead++;
}

}while (user_data && cRead < maxmembers);

user_data.close();

ifstream book_data;
book_data.open("bookdata.txt",ios::in);

do
{
book_data.getline(uTest,100,';');
book[cRead].author = uTest;
book_data.getline(uTest,100,';');
book[cRead].title = uTest;
book_data.getline(uTest,100,';');
book[cRead].category = uTest;
book_data.getline(uTest,100,'\n');
book[cRead].ISBN = uTest;

if (book_data)
{
cRead++;
}

}while (book_data && cRead < maxbooks);

book_data.close();

int choice;
cout <<"Library Menu" << endl << endl ;
cout <<"[1] List Library Members and Member Information" << endl;
cout <<"[2] List Library Books and Book Information" << endl ;
cout <<"[3] Issue Library Book" << endl ;
cout <<"[4] Return Library Book" << endl ;
cout <<"[5] Exit Library System" << endl ;
cout <<"Enter Your Required Choice" << endl;
cin >> choice;

switch(choice)
{
case 1:
member_list(user);
break;
case 2:
book_list(user);
break;
case 5:
exit(1);
break;
default:
cout << "This is not an option" << endl;
break;
}

}

Reply With Quote
  #2  
Old December 13th, 2002, 09:46 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 9 h 35 m 45 sec
Reputation Power: 4080
Your problem is here:
Code:
void member_list(cust c)
{
...<snip>..
}
...
... Some more code
....
main()
{
...
cust user[maxmembers];
...
...
case 1:
   member_list(user);
   break;

Basically, user is declared as an array (with 2000 elements) of cust, whereas your member_list() function is expecting a single cust as the argument. You can't pass the entire array to the function as an argument, when it is not expecting one. That's what the error message is about. The way I'd fix it is to change the definition of member_list() to read like this:
Code:
void member_list(cust *c) {
...
}

Also, within member_list(), you have some other errors in the code. IMHO it should be c[i].surname, c[i].house_no etc., not c.surname[i], c.house_no[i] etc. You have the same errors in book_list() as well. Hope this helps!

Reply With Quote
  #3  
Old December 15th, 2002, 06:28 PM
pschmerg pschmerg is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Blacksburg VA/Philly PA
Posts: 38 pschmerg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m
Reputation Power: 11
Send a message via AIM to pschmerg
one thing that might make things easier instead of passing the struct array through the use of a pointer, is you can pass it this way:

//in main

function_name(arrayVariable); //where function name is the fucntion you want, and arrayVariable is the variable you want



// then in your function you it should be like this

void function_name(arrayType arrayVariable[] )
{
....
}

the [] in the argument list specify that there's an array being passed and then you can use it like the person above me said. both ways work, but this might be a little easier.

Reply With Quote
  #4  
Old December 16th, 2002, 01:54 PM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
Neildadon,

Both of these suggestions amount to the same thing. You cannot push a whole structure onto the stack to pass to a function. You must us a pointer to a structure. For reasons of speed and efficiency, you'd want to use pointers anyway. Pointers are relatively small, but your data structures can be quite large. Complex data structures can also be of variable size, unlike fixed-size pointers.

Get comfortable with using pointers. You can pass anything using a pointer, and that lets you get away with some amazing stuff.

I'd highly recommend Kyle Loudon's Mastering Algorithms with C, by O'Rielly & Associates. Once you see some of the amazing things that can be pulled off with pointers, you'll have all the incentive that you need to learn how to use them.
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/

Reply With Quote
  #5  
Old February 10th, 2003, 05:55 PM
Neildadon Neildadon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Posts: 12 Neildadon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks for all your help everyone

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > passing structs to functions ?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap