Dear All
Although my tutor said that it takes 25 minus, I have been trying to understand for more than a week that how to do my task.
I have done something as you said before in terms of "putting at least some efforts".
Please see the codes below and do help me on my mistakes.
I also enclose the errors at the and of codes when I complied it.
I made semi colon mistakes some where and I did not declared some of variables.But I am stil confusing.
I need your corrections and advice
Regards

#include <string.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

const int MAX_BOOKS = 1000;
const int MAX_USERS = 500;

enum  bookCategory 
{
    Adventure = 'A', 
    Detective = 'D', 
    SciFi = 'S', 
    Romance = 'R'
};

struct Book
{
    string bookID;
    string title;
    string authorSurname;
    string authorFirstname;
    string category;
    string location;
    string onLoan;
};

struct User
{
    string userID;
    string name;
    string houseNo;
    string address;
    string noOfLoans;
};

int findBookAuthor(Book[], string, int);

int main()
{


    fstream bookDataFile;
    fstream userDataFile;
    Book books[MAX_BOOKS];
    User users[MAX_USERS];
    int noOfBooks = 0;
    int noOfUsers = 0;
    bookDataFile.open("bookdata.txt");
    userDataFile.open("userdata.txt");

    while(!bookDataFile.eof() && noOfBooks < MAX_BOOKS) 
    { 
        getline(bookDataFile, books[noOfBooks].authorSurname );
        getline(bookDataFile, books[noOfBooks].authorFirstname );
        getline(bookDataFile, books[noOfBooks].title ); 
        getline(bookDataFile, books[noOfBooks].category,);  
        getline(bookDataFile, books[noOfBooks].bookID);      
        noOfBooks++;

    }

    while(!userDataFile.eof() && noOfUsers < MAX_USERS) 
    { 
        getline(userDataFile, users[noOfUsers].name);
        getline(userDataFile, users[noOfUsers].houseNo); 
        getline(userDataFile, users[noOfUsers].address);  
        getline(userDataFile, users[noOfUsers].userID);            
        noOfUsers++;
    }
    
    string author;
    int bookID;
    int option;
    while(true)
    {
        cout << "1. Loan Book" << endl;
        cout << "2. Find Book By Author" << endl;
        cin >> option;
        switch(option)
         {
            case 1:
                cout << "Under Construction" << endl;
            break;

            case 2:
                cout << "Enter Author Name: ";
                cin >> author;
                bookID = findBookAuthor(books, author, noOfBooks);
            break;
         }
    }    
    
    return 0;
}

int findBookAuthor(Book books[], string author, int noOfBooks)
{
    for(int bookNo = 0; bookNo < noOfBooks; bookNo++)
    {    
        if(books[bookNo].authorSurname == author)
        {
            cout << "--------------------------------------------------" << endl;
            cout << "BookID: " << books[bookNo].bookID << endl;
            cout << "Title: " << books[bookNo].title << endl;
            cout << "Author: " << books[bookNo].authorSurname << ", " << books[bookNo].authorFirstname<< endl;
            cout << "Category: " << books[bookNo].category << endl;
            cout << "Location: " << books[bookNo].location << endl;
            cout << "onLoan: " << books[bookNo].onLoan << endl;
            cout << "--------------------------------------------------" << endl << endl;        
        }    
    }
    return -1;
}

int findUser(User users[], string userID, int noOfUsers)
{
    for(int bookNo = 0; bookNo < noOfBooks; bookNo++)
    {    
        if(books[bookNo].authorSurname == author)
        {
                
        }    
    }

    return -1;
}

/*
ERRORS
g++ library.cc
library.cc:20: syntax error before `;'
library.cc:21: syntax error before `;'
library.cc:22: syntax error before `;'
library.cc:23: syntax error before `;'
library.cc:24: syntax error before `;'
library.cc:25: syntax error before `;'
library.cc:26: syntax error before `;'
library.cc:31: syntax error before `;'
library.cc:32: syntax error before `;'
library.cc:33: syntax error before `;'
library.cc:34: syntax error before `;'
library.cc:35: syntax error before `;'
library.cc:38: type specifier omitted for parameter
library.cc: In function `int main()':
library.cc:50: no matching function for call to `fstream::open (const char[13])'
/usr/local/depot/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/fstream.h:89: candidates are: void fstream::open(const char *, int, int = 436)
library.cc:51: no matching function for call to `fstream::open (const char[13])'
/usr/local/depot/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/fstream.h:89: candidates are: void fstream::open(const char *, int, int = 436)
library.cc:55: `struct Book' has no member named `authorSurname'
library.cc:55: implicit declaration of function `int getline(...)'
library.cc:55: warning: cannot pass objects of type `fstream' through `...'
library.cc:56: `struct Book' has no member named `authorFirstname'
library.cc:56: warning: cannot pass objects of type `fstream' through `...'
library.cc:57: `struct Book' has no member named `title'
library.cc:57: warning: cannot pass objects of type `fstream' through `...'
library.cc:58: `struct Book' has no member named `category'
library.cc:58: parse error before `)'
library.cc:58: warning: cannot pass objects of type `fstream' through `...'
library.cc:59: `struct Book' has no member named `bookID'
library.cc:59: warning: cannot pass objects of type `fstream' through `...'
library.cc:66: `struct User' has no member named `name'
library.cc:66: warning: cannot pass objects of type `fstream' through `...'
library.cc:67: `struct User' has no member named `houseNo'
library.cc:67: warning: cannot pass objects of type `fstream' through `...'
library.cc:68: `struct User' has no member named `address'
library.cc:68: warning: cannot pass objects of type `fstream' through `...'
library.cc:69: `struct User' has no member named `userID'
library.cc:69: warning: cannot pass objects of type `fstream' through `...'
library.cc:73: `string' undeclared (first use this function)
library.cc:73: (Each undeclared identifier is reported only once
library.cc:73: for each function it appears in.)
library.cc:73: parse error before `;'
library.cc:89: `author' undeclared (first use this function)
library.cc: At top level:
library.cc:98: type specifier omitted for parameter
library.cc:98: parse error before `,'
library.cc: In function `int findBookAuthor(...)':
library.cc:100: `noOfBooks' undeclared (first use this function)
library.cc:102: `books' undeclared (first use this function)
library.cc: At top level:
library.cc:117: type specifier omitted for parameter
library.cc:117: parse error before `,' /*
*/*

Recommended Answers

All 25 Replies

Please use code tags. Otherwise it'll be hard for us to tell you on which lines your mistakes are.

most of your compile errors are in the library headers?

Are you missing a switch (compiler option)?

(I'm trying to compile it in Visual C++ Express and I'm getting an entirely different set of errors.)

change #include <string.h> to remove the .h extension -- #include <string> That will not fix all your proglems will fix some of them.

That one change fixed almost all the errors in my test project and the remaining error was so blatant I suspect you can solve it.

(If not, post your new code with your new errors and we can look at it again.)

ERROR :
at line,
58... getline(bookDataFile, books[noOfBooks].category,); 121...What is author ...i don't know, do u know?...
117-128...dont need it yet, u can remove it...

I have fixed most of your problem. But your function is just missing some identifiers(ex , insted of int x, its just x)..All i did was change your header. look at it.

#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int MAX_BOOKS = 1000;
const int MAX_USERS = 500;

enum  bookCategory 
{
    Adventure = 'A', 
    Detective = 'D', 
    SciFi = 'S', 
    Romance = 'R'
};

struct Book
{
    string bookID;
    string title;
    string authorSurname;
    string authorFirstname;
    string category;
    string location;
    string onLoan;
};

struct User
{
    string userID;
    string name;
    string houseNo;
    string address;
    string noOfLoans;
};

int findBookAuthor(Book[], string, int);

int main()
{


    fstream bookDataFile;
    fstream userDataFile;
    Book books[MAX_BOOKS];
    User users[MAX_USERS];
    int noOfBooks = 0;
    int noOfUsers = 0;
    bookDataFile.open("bookdata.txt");
    userDataFile.open("userdata.txt");

    while(!bookDataFile.eof() && noOfBooks < MAX_BOOKS) 
    { 
        getline(bookDataFile, books[noOfBooks].authorSurname );
        getline(bookDataFile, books[noOfBooks].authorFirstname );
        getline(bookDataFile, books[noOfBooks].title ); 
        getline(bookDataFile, books[noOfBooks].category),;  
        getline(bookDataFile, books[noOfBooks].bookID);      
        noOfBooks++;

    }

    while(!userDataFile.eof() && noOfUsers < MAX_USERS) 
    { 
        getline(userDataFile, users[noOfUsers].name);
        getline(userDataFile, users[noOfUsers].houseNo); 
        getline(userDataFile, users[noOfUsers].address);  
        getline(userDataFile, users[noOfUsers].userID);            
        noOfUsers++;
    }
    
    string author;
    int bookID;
    int option;
    while(true)
    {
        cout << "1. Loan Book" << endl;
        cout << "2. Find Book By Author" << endl;
        cin >> option;
        switch(option)
         {
            case 1:
                cout << "Under Construction" << endl;
            break;

            case 2:
                cout << "Enter Author Name: ";
                cin >> author;
                bookID = findBookAuthor(books, author, noOfBooks);
            break;
         }
    }    
    
    return 0;
}

int findBookAuthor(Book books[], string author, int noOfBooks)
{
    for(int bookNo = 0; bookNo < noOfBooks; bookNo++)
    {    
        if(books[bookNo].authorSurname == author)
        {
            cout << "--------------------------------------------------" << endl;
            cout << "BookID: " << books[bookNo].bookID << endl;
            cout << "Title: " << books[bookNo].title << endl;
            cout << "Author: " << books[bookNo].authorSurname << ", " << books[bookNo].authorFirstname<< endl;
            cout << "Category: " << books[bookNo].category << endl;
            cout << "Location: " << books[bookNo].location << endl;
            cout << "onLoan: " << books[bookNo].onLoan << endl;
            cout << "--------------------------------------------------" << endl << endl;        
        }    
    }
    return -1;
}

int findUser(User users[], string userID, int noOfUsers)
{
    for(int bookNo = 0; bookNo < noOfBooks; bookNo++)
    {    
        if(books[bookNo].authorSurname == author)
        {
                
        }    
    }

    return -1;
}

Dear fisrtPerson
Thank you very much for you help and effort

Here are the errors that we found.
I put two "txt" files as bookdata.txt and userdata.txt in my current directory but I think still the program can not find them.
inside the "for"function I did declare the "noOfBooks" but it gave me error.


ERRORS:
library.cc: In function `int main()':
library.cc:50: no matching function for call to `fstream::open (const char[13])'
/usr/local/depot/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/fstream.h:89: candidates are: void fstream::open(const char *, int, int = 436)
library.cc:51: no matching function for call to `fstream::open (const char[13])'
/usr/local/depot/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/fstream.h:89: candidates are: void fstream::open(const char *, int, int = 436)
library.cc:119: `noOfBooks' undeclared (first use this function)
library.cc:119: (Each undeclared identifier is reported only once
library.cc:119: for each function it appears in.)
library.cc:121: `books' undeclared (first use this function)
library.cc:121: `author' undeclared (first use this function)

I have fixed most of your problem. But your function is just missing some identifiers(ex , insted of int x, its just x)..All i did was change your header. look at it.

#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int MAX_BOOKS = 1000;
const int MAX_USERS = 500;

enum  bookCategory 
{
    Adventure = 'A', 
    Detective = 'D', 
    SciFi = 'S', 
    Romance = 'R'
};

struct Book
{
    string bookID;
    string title;
    string authorSurname;
    string authorFirstname;
    string category;
    string location;
    string onLoan;
};

struct User
{
    string userID;
    string name;
    string houseNo;
    string address;
    string noOfLoans;
};

int findBookAuthor(Book[], string, int);

int main()
{


    fstream bookDataFile;
    fstream userDataFile;
    Book books[MAX_BOOKS];
    User users[MAX_USERS];
    int noOfBooks = 0;
    int noOfUsers = 0;
    bookDataFile.open("bookdata.txt");    //Line:50
    userDataFile.open("userdata.txt");     //Line:51

    while(!bookDataFile.eof() && noOfBooks < MAX_BOOKS) 
    { 
        getline(bookDataFile, books[noOfBooks].authorSurname );
        getline(bookDataFile, books[noOfBooks].authorFirstname );
        getline(bookDataFile, books[noOfBooks].title ); 
        getline(bookDataFile, books[noOfBooks].category),;  
        getline(bookDataFile, books[noOfBooks].bookID);      
        noOfBooks++;

    }

    while(!userDataFile.eof() && noOfUsers < MAX_USERS) 
    { 
        getline(userDataFile, users[noOfUsers].name);
        getline(userDataFile, users[noOfUsers].houseNo); 
        getline(userDataFile, users[noOfUsers].address);  
        getline(userDataFile, users[noOfUsers].userID);            
        noOfUsers++;
    }
    
    string author;
    int bookID;
    int option;
    while(true)
    {
        cout << "1. Loan Book" << endl;
        cout << "2. Find Book By Author" << endl;
        cin >> option;
        switch(option)
         {
            case 1:
                cout << "Under Construction" << endl;
            break;

            case 2:
                cout << "Enter Author Name: ";
                cin >> author;
                bookID = findBookAuthor(books, author, noOfBooks);
            break;
         }
    }    
    
    return 0;
}

int findBookAuthor(Book books[], string author, int noOfBooks)
{
    for(int bookNo = 0; bookNo < noOfBooks; bookNo++)
    {    
        if(books[bookNo].authorSurname == author)
        {
            cout << "--------------------------------------------------" << endl;
            cout << "BookID: " << books[bookNo].bookID << endl;
            cout << "Title: " << books[bookNo].title << endl;
            cout << "Author: " << books[bookNo].authorSurname << ", " << books[bookNo].authorFirstname<< endl;
            cout << "Category: " << books[bookNo].category << endl;
            cout << "Location: " << books[bookNo].location << endl;
            cout << "onLoan: " << books[bookNo].onLoan << endl;
            cout << "--------------------------------------------------" << endl << endl;        
        }    
    }
    return -1;
}

int findUser(User users[], string userID, int noOfUsers)
{
    for(int bookNo = 0; bookNo < noOfBooks; bookNo++)  //Line:119
    {    
        if(books[bookNo].authorSurname == author) //Line:121
        {
                
        }    
    }

    return -1;
}

We are nearly there.We have only problems with Line: 50,51 and 119, 121.
In Line 50,51: Although I put these two files(.txt) in to the correct directory, they give errors.

In Line 119: "noOfBooks" undeclared varibale
In Line 121: "books" and "author" are undeclared.

I would be very happy if you could give me a little help again
regards

at Line :102
authorSurname==author

I'm not sure what your problem is. The code:

int findBookAuthor(Book books[], string author, int noOfBooks)
{
	for(int bookNo = 0; bookNo < noOfBooks; bookNo++)
	{ 
		if(books[bookNo].authorSurname == author)
		{

Compiles and runs on my system.

I call it from the main code like:

case 2:
			cout << "Enter Author Name: ";
			cin >> author;
			bookID = findBookAuthor(books, author, noOfBooks);
			break;

And it will find and display the book. (I think it would display multiple books, but I only have 2 books in my file.)

Dear Murtan
Thank you for your help!

I think we have problem in the "Find User" function instead of the "FundBookAuthor" function.
I think,I have something missing inside the "for" statement.

int findUser(User users[], string userID, int noOfUsers)     //-------------------
{
    for(int bookNo = 0; bookNo < noOfBooks; bookNo++)        //Line:119-----------
    {    
        if(books[bookNo].authorSurname == author)            //Line:121-----------
        {
                
        }    
    }

    return -1;
}

ERRORS:
//library.cc: In function `int findUser(User *, basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >, int)':

//library.cc:119: `noOfBooks' undeclared (first use this function)
//library.cc:119: (Each undeclared identifier is reported only once
//library.cc:119: for each function it appears in.)

//library.cc:121: `books' undeclared (first use this function)
//library.cc:121: `author' undeclared (first use this function)

What a course work! isn't it?

Haven't you learned yet to read error messages and fix problems? The first error message ways noOfBooks is not been defined on line 119. Well, what do you think is the problem? If you look at the parameter list you will see its called noOfUsers, which isn't the same. The fix: change the damed thing.

Thank you Ancient Dragon
Oh my god!.It's a silly mistake.
Because I always use copy and paste method in order to reduce working time which is wrong.
How about the error at line 121.
I did declare " books" and "author" in line 90.
regards

Alright. So for the first problem, about the compiler not finding the .txt file--Make sure you download/or include the file where your project is. This solution will be for visual 08.

1) open the project. Then on the left side of the screen(where you have your header file, source file..Right click on the name of the project and go to "open Folder in windows explorer. Then upload the file in the same address.


ok now for your other problems;

The function finduser is a function, that has 3 parameter. When you call the function you have to include 3 argument.

int findUser(User users[], string userID, int noOfUsers)

In your int main()
{
when you call it (ex. findUser(someArray, someString, someInt)
{
you have to use either someArray, someString, or someInt in your function. That is unless you declare another variable in your function, or have a global variable.

so try using the variables declared in the function's parameter.

hope that helps somewhat...

and Merry Christmas.

Books and author may have been defined on line 90, but that was inside another function. Line 121 is in a different function, so the variables are undefined. (This concept is called the scope of a variable.)

But could you explain why in the findUser function you're looking at the books anyway?

Dear All
First of all, I would like to say thank you to helping me so far.
My program should:

define appropriate user-defined data types to model the data of the application.

read the book data from the book data file and store it in a convenient form.

read the user data from the user data file and store it in a convenient form.

allow a book to be issued to a user or returned by a user.

allow the library staff to list all books by an author, to report which are currently in stock or on loan and to indicate in which area of the library they may be found.
I think we are nearly there but there are still two problems(errors)
which are make me unhappy.
The first error seen at line 50-51 is the recognition problem of seperate files in my directory called "bookdata.txt" and "userdata.txt"
(I have attached them too with this message.)
The second error is seen at the line 121.
Where do I need to make changes and what purpose?
Merry Christmas to all

#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int MAX_BOOKS = 2000;
const int MAX_USERS = 500;

enum  bookCategory 
{
    Adventure = 'A', 
    Detective = 'D', 
    SciFi = 'S', 
    Romance = 'R'
};

struct Book
{
    string bookID;
    string title;
    string authorSurname;
    string authorFirstname;
    string category;
    string location;
    string onLoan;
};

struct User
{
    string userID;
    string name;
    string houseNo;
    string address;
    string noOfLoans;
};

int findBookAuthor(Book[], string, int);

int main()                                         //-----------------
{


    fstream bookDataFile;
    fstream userDataFile;
    Book books[MAX_BOOKS];
    User users[MAX_USERS];
    int noOfBooks = 0;
    int noOfUsers = 0;
    bookDataFile.open("bookdata.txt");            //Line:50----------
    userDataFile.open("userdata.txt");            //Line:51----------

    while(!bookDataFile.eof() && noOfBooks < MAX_BOOKS) 
    { 
        getline(bookDataFile, books[noOfBooks].authorSurname );
        getline(bookDataFile, books[noOfBooks].authorFirstname );
        getline(bookDataFile, books[noOfBooks].title ); 
        getline(bookDataFile, books[noOfBooks].category);
        getline(bookDataFile, books[noOfBooks].bookID);      
        noOfBooks++;

    }

    while(!userDataFile.eof() && noOfUsers < MAX_USERS) 
    { 
        getline(userDataFile, users[noOfUsers].name);
        getline(userDataFile, users[noOfUsers].houseNo); 
        getline(userDataFile, users[noOfUsers].address);  
        getline(userDataFile, users[noOfUsers].userID);            
        noOfUsers++;
    }

    string author;
    int bookID;
    int option;
    while(true)
    {
        cout << "1. Loan Book" << endl;
        cout << "2. Find Book By Author" << endl;
        cin >> option;
        switch(option)
         {
            case 1:
                cout << "Under Construction" << endl;
            break;

            case 2:
                cout << "Enter Author Name: ";
                cin >> author;
                bookID = findBookAuthor(books, author, noOfBooks);
            break;
         }
    }    

    return 0;
}

int findBookAuthor(Book books[], string author, int noOfBooks)
{
    for(int bookNo = 0; bookNo < noOfBooks; bookNo++)
    {    
        if(books[bookNo].authorSurname == author)
        {
            cout << "--------------------------------------------------" << endl;
            cout << "BookID: " << books[bookNo].bookID << endl;
            cout << "Title: " << books[bookNo].title << endl;
            cout << "Author: " << books[bookNo].authorSurname << "," << books[bookNo].authorFirstname<< endl;
            cout << "Category: " << books[bookNo].category << endl;
            cout << "Location: " << books[bookNo].location << endl;
            cout << "onLoan: " << books[bookNo].onLoan << endl;
            cout << "--------------------------------------------------" << endl << endl;        
        }    
    }
    return -1;
}

int findUser(User users[], string userID, int noOfUsers)                 //-------------------
{
    for(int userID = 0; userID < noOfUsers; userID++)
    {    
        if(users[userID].name == users)                                  //Line:121-----------
        {
            cout << "--------------------------------------------------" << endl;
            cout << "UserID: " << users[userID].userID << endl;
            cout << "Name: " << users[userID].name << endl;
            cout << "House No: " << users[userID].houseNo << endl;
            cout << "Address: " << users[userID].address << endl;
            cout << "No of Loans: " << users[userID].noOfLoans << endl;
            cout << "--------------------------------------------------" << endl << endl;  
        }    
    }

    return -1;
}
/*
1 jaguar% g++ library.cc
library.cc: In function `int main()':
library.cc:50: no matching function for call to `fstream::open (const char[13])'
/usr/local/depot/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/fstream.h:89: candidates are: void fstream::open(const char *, int, int = 436)
library.cc:51: no matching function for call to `fstream::open (const char[13])'
/usr/local/depot/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/fstream.h:89: candidates are: void fstream::open(const char *, int, int = 436)
library.cc: In function `int findUser(User *, basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >, int)':
library.cc:121: no match for `string & == User *&'
/*
commented: Should've figured out code tags by now - move along, nothing to see here. -5

please, use code=c++ tags so it will number your lines

int findUser(User users[], string userID, int noOfUsers) //-------------------
{
for(int userID = 0; userID < noOfUsers; userID++)
{ 
if(users[userID].name == users) //Line:121-----------
{

The error on line 121 reads: no match for `string & == User *&' so the compiler thinks you're trying to compare a string and a pointer (or array) of users. Wait...that IS what you asked it to do.

You have repeated a previously mentioned 'bad form' and have declared (on line 119) an int userID that 'hides' the string userID from line 117.

The function prototype seems to indicate that you want to search for a user by ID, but you are trying to compare to the users's name? Which field are you supposed to be searching on?

The calls to open confuse me. I'm doing this in my program (it works with my original data file, I'll try it with yours too.):

fstream bookDataFile;
	Book books[MAX_BOOKS];
	bookDataFile.open("bookdata.txt");
	int noOfBooks = 0;
	while(!bookDataFile.eof() && noOfBooks < MAX_BOOKS) 
	{ 
		getline(bookDataFile, books[noOfBooks].authorSurname);
		getline(bookDataFile, books[noOfBooks].authorFirstname);
		getline(bookDataFile, books[noOfBooks].title);
		getline(bookDataFile, books[noOfBooks].category);
		getline(bookDataFile, books[noOfBooks].bookID);
		if (! bookDataFile.eof())
			noOfBooks++;
	}
	bookDataFile.close();

You might see if bookDataFile.open("bookdata.txt", ios_base::in); works any better for you, your library seems to want a second argument. Alternatively, you could try declaring the streams as ifstreams ifstream bookDataFile; maybe that would make it work without the second argument.

After looking at your data files, your read routines will need work too. The read routines seem to imply one piece of data per line, but the files are one record per line with ';' delimiters:

Samples from bookdata.txt:

Forsyth, Frederick      ;The fist of god               ;A;102391
Follett, Ken            ;The pillars of the earth      ;A;103795

Samples from userdata.txt

achillios           ; 27;old road                 ;10096
bains               ;  3;cirrus crescent          ;24397

Dear Murtan
I have not used DANIWEB Discussion platform before,sorry for any inconvenience.

YES YES YES
Finally we could manage to solve reading two external files by replacing "fstream bookDataFile" with "ifstream bookDataFile".
That's great.But still there is a minor problem at Line 126 (see below).
Thank you very much again, you helped me too much.
regards

int findUser(User users[], string userID, int noOfUsers)                 
{
    for(int userID = 0; userID < noOfUsers; userID++)
    {    
        if(users[userID].name == users)          // line 126.                                 
        {
            cout << "--------------------------------------------------" << endl;
            cout << "UserID: " << users[userID].userID << endl;
            cout << "Name: " << users[userID].name << endl;
            cout << "House No: " << users[userID].houseNo << endl;
            cout << "Address: " << users[userID].address << endl;
            cout << "No of Loans: " << users[userID].noOfLoans << endl;
            cout << "--------------------------------------------------" << endl << endl;  
        }    
    }

    return -1;
}

ERROR MESSAGE FOR LINE 126:
library.cc: In function `int findUser(User *, basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >, int)':
library.cc:126: no match for `string & == User *&'

You apparently know about using {code}{/code} (where you use square brackets instead of the braces). When posting C++ code, just make it look like {code=c++}{/code} (using square brackets). (I've seen people post what it's really supposed to look like, but I haven't figured out how to make it work yet.)

Now on to your current problem:

From my last reply on this topic:

The error on line 121 reads: no match for `string & == User *&' so the compiler thinks you're trying to compare a string and a pointer (or array) of users. Wait...that IS what you asked it to do.

You have repeated a previously mentioned 'bad form' and have declared (on line 119) an int userID that 'hides' the string userID from line 117.

The function prototype seems to indicate that you want to search for a user by ID, but you are trying to compare to the users's name? Which field are you supposed to be searching on?

That still applies to the current code. I'll try to point it out by adding comments.

// Here, we declare the "findUser" function with the following parameter:
//    the list of users "users" 
//    a "userID" 
//    the number of users "noOfUsers"
int findUser(User users[], string userID, int noOfUsers)                 
{
    // Now we declare a for loop to iterate over all of the users
    // we create the new variable "userID" to be the array index
    // NOTE: this is the SAME NAME as the parameter above
    // so from this point on, the parameter "userID" is no-longer visible
    for(int userID = 0; userID < noOfUsers; userID++)
    {    
        // Here we compare the name of the user in the "userID" position
        // (the array index one) with "users"...
        // wait...what is users again? ... let me look up....
        // oh, that's right, that's the name of the parameter that
        // contains the whole list of users...
        // so we're asking it to compare one user's name with the whole list of users?
        // That confuses me, no wonder it confuses the compiler.
        if(users[userID].name == users)          // line 126
        {
            cout << "--------------------------------------------------" << endl;
            cout << "UserID: " << users[userID].userID << endl;
            cout << "Name: " << users[userID].name << endl;
            cout << "House No: " << users[userID].houseNo << endl;
            cout << "Address: " << users[userID].address << endl;
            cout << "No of Loans: " << users[userID].noOfLoans << endl;
            cout << "--------------------------------------------------" << endl << endl;  
        }    
    }

    return -1;
}

The solution is to not use the same name in the for loop as the parameter name. Either change the variable in the for loop (and everywhere it is used), or change the parameter name from userID to userName. The choice depends on whether this function is supposed to search for users by name or by user id.

Once you make that decision, you can either compare users[...].userID with userID or users[...].name with userName.

Alternatively, you could just comment the whole function out as you are not calling it. (At least in the last code you posted.)

NOTE: as noted in my last message about the file format, unless you have changed your read routines, they will not successfully read the data in your data files. The read routines expect the data fields to be one per line, but the fields are all on one line with delimiters. As a minimal modification to your read routine, it works pretty well by just passing the appropriate delimiter to the read function like this: getline(bookDataFile, books[noOfBooks].authorSurname, ','); you will need to modify the remaining getline calls to include the proper delimiter. (The last call will not need to be modified, it will use the end-of-line by default.)

#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int MAX_BOOKS = 2000;
const int MAX_USERS = 500;

enum  bookCategory 
{
    Adventure = 'A', 
    Detective = 'D', 
    SciFi = 'S', 
    Romance = 'R'
};

struct Book
{
    string bookID;
    string title;
    string authorSurname;
    string authorFirstname;
    string category;
    string location;
    string onLoan;
};

struct User
{
    string userID;
    string name;
    string houseNo;
    string address;
    string noOfLoans;
};

int findBookAuthor(Book[], string, int);

int main()                                         
{


    ifstream bookDataFile;
    ifstream userDataFile;
    Book books[MAX_BOOKS];
    User users[MAX_USERS];
    int noOfBooks = 0;
    int noOfUsers = 0;
    bookDataFile.open("bookdata.txt");            
    userDataFile.open("userdata.txt");            

    while(!bookDataFile.eof() && noOfBooks < MAX_BOOKS) 
    { 
        getline(bookDataFile, books[noOfBooks].authorSurname);
        getline(bookDataFile, books[noOfBooks].authorFirstname);
        getline(bookDataFile, books[noOfBooks].title); 
        getline(bookDataFile, books[noOfBooks].category);
        getline(bookDataFile, books[noOfBooks].bookID);
        if (! bookDataFile.eof())      
              noOfBooks++;
    }

    bookDataFile.close();

    while(!userDataFile.eof() && noOfUsers < MAX_USERS) 
    { 
        getline(userDataFile, users[noOfUsers].name);
        getline(userDataFile, users[noOfUsers].houseNo); 
        getline(userDataFile, users[noOfUsers].address);  
        getline(userDataFile, users[noOfUsers].userID);
        if (! userDataFile.eof())             
        noOfUsers++;
    }

    bookDataFile.close();

    string author;
    int bookID;
    int option;
    while(true)
    {
        cout << "1. Loan Book" << endl;
        cout << "2. Find Book By Author" << endl;
        cin >> option;
        switch(option)
         {
            case 1:
                cout << "Under Construction" << endl;
            break;

            case 2:
                cout << "Enter Author Name: ";
                cin >> author;
                bookID = findBookAuthor(books, author, noOfBooks);
            break;
         }
    }    

    return 0;
}

int findBookAuthor(Book books[], string author, int noOfBooks)
{
    for(int bookNo = 0; bookNo < noOfBooks; bookNo++)
    {    
        if(books[bookNo].authorSurname == author)
        {
            cout << "--------------------------------------------------" << endl;
            cout << "BookID: " << books[bookNo].bookID << endl;
            cout << "Title: " << books[bookNo].title << endl;
            cout << "Author: " << books[bookNo].authorSurname << "," << books[bookNo].authorFirstname<< endl;
            cout << "Category: " << books[bookNo].category << endl;
            cout << "Location: " << books[bookNo].location << endl;
            cout << "onLoan: " << books[bookNo].onLoan << endl;
            cout << "--------------------------------------------------" << endl << endl;        
        }    
    }
    return -1;
}

int findUser(User users[], string [COLOR="red"]userName[/COLOR], int noOfUsers)                 
{
    for(int userID = 0; userID < noOfUsers; userID++)
    {    
        if(users[[COLOR="Red"]userName[/COLOR]].name == users)                                // line 126-----------------------                                 
        {
            cout << "--------------------------------------------------" << endl;
            cout << "UserID: " << users[userID].userID << endl;
            cout << "Name: " << users[userID].name << endl;
            cout << "House No: " << users[userID].houseNo << endl;
            cout << "Address: " << users[userID].address << endl;
            cout << "No of Loans: " << users[userID].noOfLoans << endl;
            cout << "--------------------------------------------------" << endl << endl;  
        }    
    }

    return -1;
}

Dear Murtan
I have made the changes as you specified but it still gives me same error as seen below(in red).I can't see where is my mistake(s).
I also put commas (',') in getline calls but they gave me errors as well so I discarded them.

I know,Today is xmas and I give you headache,sorry about that!
I could not sleep last night just because of this silly error.
Merry Christmas

ERROR:
1 jaguar% g++ library.cc
library.cc: In function int findUser(User *, basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >, int)': library.cc:126: no match forUser *&[string &]'

>>if(users[userName].name

UserName is std::string, not an int. My guess is that the function need to search the User array for the username, then use that index number in the quoted line.

It's to the point now, where I wonder if you aren't intentionally trying to not understand. I don't believe that I've ever had this much trouble getting someone to 'get it', even when I was working in the computer lab helping first semester programmers all those years ago.

Based on the code you have posted, findUser() is now a 'search for user by name'. In that case, I told you to

Once you make that decision, you can either compare users[...].userID with userID or users[...].name with userName.

but you changed the compare to do something else entirely, you replaced the array index with the name (which makes no sense, no wonder the compiler complained).

As userID is the name of one of the members of the struct User, it makes a really bad name for a variable to index the array that contains instances of the User struct. Change the for loop and compare to the following:

for(int ii = 0; ii < noOfUsers; ii++)
{
    if  (users[ii].name == userName)
    {
        cout << "--------------------------------------------------" << endl;
        cout << "UserID: " << users[ii].userID << endl;
        // YOU put the rest of the print statments in
    }
}

As for the read routine that doesn't work, I refuse to help you any more with it until you post some code that compiles that shows you made an effort to solve the problem yourself.

And if you post your code again without using [code=c++] // your code goes here

[/code] tags again I'll unsubscribe the thread.

Thanks sir
you helped me so much
merry christmas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.