passing structs to functions?

This is a discussion on passing structs to functions? within the C++ Programming forums, part of the General Programming Boards category; hi im having a few problems with this program im making. Its a library program being made in unix g++. ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    15

    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 members of the library database. Im reading this data from a text file.

    can anyone help im really desperate ??
    Code:
    #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;
       }
    
    }


    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Two things, first you are trying to pass struct type cust through your function book_list, which takes as a parameter books (NOT cust).

    Second, you are trying to pass arrays when the two functions only take single objects. Need to change declarations to:
    Code:
    void member_list(cust c[])
    void book_list(books b[])
    [edit] Ya beat me Salem! [/edit]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 12:31 PM
  2. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 04:13 PM
  3. passing pointers at structs thru functions
    By nunnu in forum C Programming
    Replies: 2
    Last Post: 05-12-2004, 10:16 AM
  4. Passing structs
    By MethodMan in forum C Programming
    Replies: 5
    Last Post: 04-05-2003, 11:15 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21