Cannot find author surname and forename

Hi I have created a program for a public library which should find a book by author, but I am having problems with entering an author. When I enter the surname and forename, it just displays the option 'Enter Author Surname and Forename'. Can you help me solve this so it will help me find an author and display the books by the author? My code is shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

#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;

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

            case 2:
	      cout << "Enter the Author's Surname and Forename: " << endl;
	      getline(cin, author, '\n');
                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[userID].name == userName)                                                                
        {
            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;
}




Thanks if you can help with my problem.
Last edited on
Line 73: After you enter the option, it leaves the \n in the input stream. So line 83 is immediately submitted.

Read http://www.cplusplus.com/forum/articles/6046/ and modify line 73 to use a better method if getting the option and this'll fix your issue.
Thanks for your reply, but I honestly dont get how to implement what is said in the link to my program. Can you explain it further to me?
Instead of cin, you use an std::string and getline().

1
2
3
4
5
6
7
//instead of this:
string input;
cin>>input;

//use this:
string input;
getline(cin, input);
So do I need to take it out of my case statement 2?
Re-Read the link I posted, and look at how it loads the number in. The problem with using >> is that it'll leave the \n in the stream. So you can either use a cin.ignore() OR modify it to the way in the post which will also add some validation against bad input for you.

Topic archived. No new replies allowed.