Thread: Help me solve this programming error please

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    42

    Help me solve this programming error please

    I have created the following code and I need some guidance on how to correct my error. The code is as follows:

    Code:
    #include <string>
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    
    const int MAX_BOOKS = 2000;
    const int MAX_USERS = 500;
    
    struct User
    {
        string userID;
        string name;
        string houseNo;
        string address;
        string noOfLoans;
    };
    
    struct Book
    {
        string bookID;
        string title;
        string authorSurname;
        string authorFirstname;
        string category;
        string location;
        string onLoan;
    };
    
    enum  bookCategory 
    {
        Adventure = 'A', 
        Detective = 'D', 
        SciFi = 'S', 
        Romance = 'R'
    };
    
    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();
    
        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 userName, int noOfUsers)                 
    {
        for(int userID = 0; userID < noOfUsers; userID++)
        {    
            if(users[userName].name == users)                                                                 
            {
                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 error I am getting is around this area:

    Code:
    for(int userID = 0; userID < noOfUsers; userID++)
        {    
            if(users[userName].name == users)                                                                 
            {
                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;
    And the error message I am receiving is:

    Code:
    In function `int findUser(User *, basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >, int)':
    library.cc:114: no match for `User *&[string &]'
    Can someone please guide me on how to fix this problem, thanks in advance.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> users[userName]
    users is an array accessed by a number. userName is a string. Should be users[userID], I'd imagine.

    Also, it might be worth using vectors instead of arrays. They're expandable arrays, not limited to the bounds you set at the start.
    Last edited by twomers; 01-03-2009 at 10:11 AM.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What is the type of each variable in the following? Does that make sense to you?

    Code:
    if(users[userName].name == users)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Sorry I require more help than this.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    look at the variable you're using to index your array in that if statment. I think you'll find that it's not compatible with the type of variable that C++ would normally expect for an array index.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by Elkvis View Post
    look at the variable you're using to index your array in that if statment. I think you'll find that it's not compatible with the type of variable that C++ would normally expect for an array index.
    So what should be there instead? Sorry I can't exactly understand it, just feeling a little ill and can usually correct these things, but today I just want someone to help me so I can get on with the rest of it when I am better.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rushhour View Post
    So what should be there instead? Sorry I can't exactly understand it, just feeling a little ill and can usually correct these things, but today I just want someone to help me so I can get on with the rest of it when I am better.
    I realize twomers has a Calvin and Hobbes avatar, but you should probably pay attention to what the man says anyway.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by twomers View Post
    >> users[userName]
    users is an array accessed by a number. userName is a string. Should be users[userID], I'd imagine.

    Also, it might be worth using vectors instead of arrays. They're expandable arrays, not limited to the bounds you set at the start.
    Can you please help me with a solution to solve this problem them. I'm sorry but I don't want to use vectors, I at least want to try solving the problem this way before using vectors. I also tried it using users[userID] and it hasn't work.

    Thanks if you can help.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rushhour View Post
    Can you please help me with a solution to solve this problem them. I'm sorry but I don't want to use vectors, I at least want to try solving the problem this way before using vectors. I also tried it using users[userID] and it hasn't work.

    Thanks if you can help.
    Did you also manage to change ==users into ==userName?

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Thanks it works now, but now I am getting a further problem. Every time I enter an author's name in the format Brook, Frederick it seems that the program goes into a continuous mode of displaying the menu options until i press control D to stop it.

    Any ideas on how to solve this?

    Thanks if you can help.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rushhour View Post
    Thanks it works now, but now I am getting a further problem. Every time I enter an author's name in the format Brook, Frederick it seems that the program goes into a continuous mode of displaying the menu options until i press control D to stop it.

    Any ideas on how to solve this?

    Thanks if you can help.
    Use a real input mechanism, as opposed to >> (that is, at least use something that can handle spaces).

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by tabstop View Post
    Use a real input mechanism, as opposed to >> (that is, at least use something that can handle spaces).
    And what would that be? (sorry I'm asking alot, my illness doesn't help me concentrate, but i'd rather get it done now)

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rushhour View Post
    And what would that be? (sorry I'm asking alot, my illness doesn't help me concentrate, but i'd rather get it done now)
    Well, you used getline elsewhere; why not here?

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by tabstop View Post
    Well, you used getline elsewhere; why not here?
    Ok thanks, I did use this, but I honestly forget how to use this getline command can you tell me how to use this to solve the problem of continuing to display the menu options untill i press control d?

    Thanks if you can help.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rushhour View Post
    Ok thanks, I did use this, but I honestly forget how to use this getline command can you tell me how to use this to solve the problem of continuing to display the menu options untill i press control d?

    Thanks if you can help.
    You could look back at your very own program and read it (after all you wrote it, right?) to see how to use getline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can anyone solve this for me? (look inside)
    By johnsonswww in forum C Programming
    Replies: 10
    Last Post: 03-02-2009, 11:24 AM
  2. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  3. How to handle in software to solve this block diagram?
    By vnrabbit in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 02:45 PM
  4. Help to solve this problem
    By Romashka in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2002, 09:32 AM