Can anyone explain this BUG and how to fix it?!

So,if you were to run the program, after entering the file name again after the file had been created... before the menu is shown it seems to display my create book function (void Book::input(istream& ins)???. I have to hit enter again for it to then display the menu. Any help would be appreciated!

javascript:tx('code')

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <string>
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;

class Book    // Implementing Book Class
{


public:
    Book();
    Book(string New_BookName, string New_AuthorName, string New_Genre, int New_BookNumber); // Declaring variables in book class
    ~Book();

    string get_BookName() const{return Name;};     
    string get_AuthorName()const{return Author; };
    void output(std::ostream& outs);   
    void input(std::istream& ins);

    void StoreBook(istream & ins);

private:
    string Name;    
    string Author;
    string Genre;
    int ID;

};
std::ostream& operator <<(std::ostream& outs, Book& bk);  
std::istream& operator >>(std::istream& ins, Book& bk);

Book::Book()   
{
    ID = 0;  

}
Book::Book(string New_BookName, string New_AuthorName, string New_Genre, int New_BookNumber)
{
    Name = New_BookName;   
    Author = New_AuthorName;
    Genre = New_Genre;
    ID = New_BookNumber;

}
Book::~Book()
{
}
void Book::output(ostream& outs) 
{


    outs << "Name: "<< Name << endl;  
    outs << "ID: "<<ID << endl;
    outs << "Genre: "<<Genre << endl;
    outs <<"Author: " <<Author << endl;


}
void Book::input(istream& ins) 
{

    {
        cin.ignore(); 
        cout << " Enter Book Name:" << endl; 
        ins >> Name; 
        cout << "Enter Author Name:" << endl;
        ins >> Author;
        cout << " Enter the Genre of the Book" << endl;
        ins >> Genre;
        cout << "Enter ID:" << endl;
        ins >> ID;




    }


}

void Book::StoreBook(istream& ins)
{

    cout << " Enter Book Name:" << endl;
    getline(ins, Name);
    cout << "Enter Author Name:" << endl;
    ins >> Author;

}
ostream& operator <<(ostream& outs, Book& bk)
{
    bk.output(outs);   
    return outs;
}
istream& operator >>(istream& ins, Book& bk)
{
    bk.input(ins);      /
    return ins;
}

class Database   
{

public:
    Database();
    ~Database();
    Database(const Database& other);
    void operator =(const Database& other);
    void search(std::string name);
    void add(const Book& bk);
    void displayall();
    void remove(std::string name);
    void save(std::ostream& outs);
    void load(std::istream& ins);


private:
    int capacity;
    Book *data;
    int used;

protected:





};

Database::Database()
{
    used = 0;
    capacity = 5;
    data = new Book[capacity];
}
Database::Database(const Database& other)
{
    used = other.used;
    capacity = other.capacity;
    data = new Book[capacity];
    copy(other.data,other.data+used,data);
}
Database::~Database()
{
    delete[]data; //deletes pointer that has went out scope
}
void Database::operator =(const Database& other)
{
    if(&other == this)
    {
        return;
    }
    delete[]data;
    capacity = other.capacity;
    used = other.used;
    data = new Book[capacity];
    copy(other.data,other.data+used,data);
}



void Database::search(std::string name)
{
    int UserFound = 0;
    for(int i=0; i<used;i++)
    {
        if(data[i].get_BookName() == name)
        {
            cout << "Book is available in library" << endl;
            data[i].output(cout);
            UserFound++;
        }
    }
    if(UserFound == 0)
    {
        cout << "That Book is not available in the library. Please request book from librarian!" << endl;
    }
}
void Database::add(const Book& bk)
{


    data[used] = bk;
    used++;
}
void Database::displayall()
{
    for(int i=0;i<used;i++)
    {
        data[i].output(cout);
    }
}
void Database::remove(std::string name)
{
    name + " Taken out";
}
void Database::save(std::ostream& outs)
{

    for(int i=0; i<used; i++)
    {
        outs << data[i];
    }
}
void Database::load(std::istream& ins)
{
    Book bk;
    while(ins >> bk)
    {

        data[used] = bk;
        used++;
    }
}

// INcluding c++ Libraries


Book bk;
Database database;
string filename;
// creating a variable for choice which will allow the user to choose an option
// Declaring my global variable

int menu();
int main()
{
    Database database;
    Book bk;
    string filename;
    cout << " Guide: Enter in a file name with a .txt when prompted" << endl;
    cout << " Once the program creates the file... Enter in the same file name to access that file and hit Enter twice" << endl;
    cout << " BUG: When you are shown with: Enter ID: .... Hit enter to be shown with the menu." << endl;
    cout << "Enter name of file and add .txt: ";
    getline(cin, filename);

    ifstream filein(filename.c_str());
    if (!filein.fail())
    {
        database.load(filein);
        filein.close();
        int choice= 0;
        while (choice != 6)
        {

            cin.ignore();
            choice = menu();
            cin >> choice;
            switch (choice)
            {
                case 1: {
                    cin >> bk;
                    database.add(bk);
                    break;
                }
                case 2: {

                    string name;
                    cout << "-----------------------------------------" << endl;
                    cout << "Enter name of the book you want to find: " << endl;
                    cout << "-----------------------------------------" << endl;
                    if (cin.peek() == '\n')cin.ignore();
                    getline(cin, name);
                    database.search(name);
                    break;
                }
                case 3: {

                    int id;
                    cout << "--------------------------" << endl;
                    cout << "Enter Book  ID number: " << endl;
                    cout << "--------------------------" << endl;
                    cin >> id;
                    //

                    break;
                }
                case 4: {

                    cout << "------------" << endl;
                    cout << "The Library" << endl;
                    cout << "------------" << endl;
                    database.displayall();
                    break;
                }
                case 5: {

                    string name;
                    cout << "--------------------------" << endl;
                    cout << "Enter Book Name to remove " << endl;
                    cout << "--------------------------" << endl;
                    if (cin.peek() == '\n')cin.ignore();
                    getline(cin, name);

                    break;
                }
                case 6: {

                    cout << " Program terminating" << endl;
                    break;

                    return 0;

                }
                default: {


                    cout << " ERROR Please enter in a number between 1-6" << endl;
                    break;
                    break;
                    break;
                    cin.ignore();
                    return 0;
                }
            }
        }
        ofstream fout(filename.c_str());
        if (!fout.fail())
        {
            database.save(fout);
        }
        else {
            cout << "ERROR FILE CANNOT BE OPENED" << endl;
        }
        fout.close();

    }
    else {
        cout << "CREATING FILE!" << endl;
        ofstream new_file(filename.c_str());
        new_file.close();
        main();
    }
    return 0;
}
int menu() // creating a separate function for displaying the menu and asking for user input
{
    int choice = 0; // creating a variable for choice which will allow the user to choose an option
    cout << "1. Add New Book." << endl;
    cout << "2. Search for a Book by name." << endl;
    cout << "3. Search for a Book by ID number." << endl;
    cout << "4. Display all Books in Library." << endl;
    cout << "5. Remove a Book from Library." << endl;
    cout << "6. Quit" << endl;

    return choice; // returning choice so i can use this value in my Int Main
}
I would think that line 65 is a problem. If there is no character in the stream it waits for one and discard it. So move line 65 after line 73.
remove line 3. that's the first problem line I saw.
Tried both your solutions, didn't fix the strange output :/
~/src/forums/ggrules$ g++ ggrules.cpp -o ggrules
ggrules.cpp: In function ‘std::istream& operator>>(std::istream&, Book&)’:
ggrules.cpp:98:25: error: expected primary-expression before ‘/’ token
     bk.input(ins);      /
                         ^
ggrules.cpp:99:5: error: expected primary-expression before ‘return’
     return ins;
     ^~~~~~

Was there a comment in the code that got partially erased when you pasted it into the forum or did your compiler just ignore the spurious / ?

I'll try it out and see what I can get it to do..

first run thru, the output looped infinitely after ERROR please enter a number cause I didn't do what it said, because it didn't ask me what to do until it was too late.

I went to fix this in the code and I see there's a subroutine choice that gets called but it always returns the same thing, and in one spot you use cin later to set it. why not use the cin within the choice, and return the value from choice like it seems you originally wanted to?
Last edited on
Sorry, yeah that was comment... just ignore that haha
Topic archived. No new replies allowed.