I am trying to search for a book in my array by title or ISBN. However, I cannot get it to find the books already in the array using my search function. I have no idea why it is not working. I thought the code was correct. The search works for ISBN for the first couple of entries then fails for the rest. However, the search fails for all the titles. Since the code is pretty much the same for each I have no idea why it is partly working for one and not at all for the other. Any tips to get the search to work correctly?
// BooksProgram.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <string>
#include "Book_Class.h"
#include <fstream>
#include <cctype>
using namespace std;
void readData(Book_Class books[]);
void MainMenu(int& menu);
bool searchLibrary(Book_Class books[]);
int main()
{
int menu = 0;
Book_Class books[100];
readData(books);
while (menu != 5)
{
MainMenu(menu);
if (menu == 1)
{
searchLibrary(books);
}
else if (menu == 2)
{
}
else if (menu == 3)
{
}
else if (menu == 4)
{
}
else if (menu == 5)
{
}
}
}
void readData(Book_Class books[])
{
ifstream in;
string line;
int numAuthors = 0;
int i = 0;
int year = 0;
double price = 0;
int copies = 0;
string author1;
string author2;
string author3;
string author4;
in.open("Books.txt");
if (in.fail())
{
cout << "File did not open." << endl;
}
while (!in.eof())
{
getline(in, line);
numAuthors = stoi(line);
books[i].setNumAuthors(numAuthors);
if (numAuthors >= 5)
{
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 4)
{
getline(in, line);
author1 = line;
getline(in, line);
author2 = line;
getline(in, line);
author3 = line;
getline(in, line);
author4 = line;
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 3)
{
getline(in, line);
author1 = line;
getline(in, line);
author2 = line;
getline(in, line);
author3 = line;
author4 = "";
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 2)
{
getline(in, line);
author1 = line;
getline(in, line);
author2 = line;
author3 = "";
author4 = "";
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
else if (numAuthors == 1)
{
getline(in, line);
author1 = line;
author2 = "";
author3 = "";
author4 = "";
books[i].setAuthor(author1, author2, author3, author4);
getline(in, line);
books[i].setTitle(line);
getline(in, line);
books[i].setISBN(line);
getline(in, line);
books[i].setPublisher(line);
getline(in, line);
year = stoi(line);
books[i].setYear(year);
getline(in, line);
price = stod(line);
books[i].setPrice(price);
getline(in, line);
copies = stoi(line);
books[i].setInStock(copies);
}
i++;
}
in.close();
}
void MainMenu(int& menu)
{
cout << setw(20) << "Main Menu" << endl;
cout << setw(36) << setfill('*') << "\n" << endl;
cout << "1. Search for book by Title or ISBN." << endl;
cout << "2. Add a new book to inventory." << endl;
cout << "3. Update the number of copies on hand." << endl;
cout << "4. Update the price of a book." << endl;
cout << "5. Exit" << endl;
cout << setw(36) << "\n" << endl;
cout << "Please enter a menu number to proceed: " << endl;
cin >> menu;
while (menu != 1 && menu != 2 && menu != 3 && menu != 4 && menu != 5)
{
cout << "Menu options are 1 - 6" << endl;
cout << "Please enter a menu number to proceed: " << endl;
cin.clear();
cin.ignore(100, '\n');
cin >> menu;
}
cout << setfill(' ') << "\n" << endl;
}
bool searchLibrary(Book_Class books[])
{
int search;
int i = 0;
int numAuthors = 0;
bool flag = false;
string title;
string ISBN;
cout << "Please enter 1 to search by Title or 2 to search by ISBN: " << endl;
cin >> search;
while (search != 1 && search != 2)
{
cout << "Invalid Selection." << endl;
cout << "Please enter 1 to search by Title or 2 to search by ISBN:" << endl;
cin.clear();
cin.ignore(100, '\n');
cin >> search;
}
if (search == 1)
{
cout << "Please enter the title of the book: " << endl;
cin >> title;
while (i < sizeof(books) -1)
{
if (title == books[i].getTitle())
{
flag = true;
books[i].showTitle();
numAuthors = books[i].getNumAuthors();
books[i].showAuthor(numAuthors);
books[i].showPublisher();
books[i].showYear();
books[i].showPrice();
books[i].showInStock();
break;
}
else
{
flag = false;
i++;
}
}
}
else if (search == 2)
{
cout << "Please enter the ISBN: " << endl;
cin >> ISBN;
while (i < sizeof(books) - 1)
{
if (ISBN == books[i].getISBN())
{
flag = true;
books[i].showTitle();
numAuthors = books[i].getNumAuthors();
books[i].showAuthor(numAuthors);
books[i].showPublisher();
books[i].showYear();
books[i].showPrice();
books[i].showInStock();
break;
}
else
{
flag = false;
i++;
}
}
}
if (!flag)
{
cout << "The book is not in stock." << endl;
}
return flag;
}
Here is my class:
#pragma once
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
class Book_Class
{
private:
string title;
string ISBN;
string publisher;
double price;
int year;
string author1;
string author2;
string author3;
string author4;
int inStock;
int numAuthors;
public:
void showTitle()
{
cout << left << setw(10) << left << setw(12);
cout << "Title: " << title << endl;
}
string getTitle()
{
return title;
}
string getISBN()
{
return ISBN;
}
void setTitle(string bookTitle)
{
title = bookTitle;
}
void showISBN()
{
cout << left << setw(10) << left << setw(12);
cout << "ISBN: " << ISBN << endl;
}
void setISBN(string isbn)
{
ISBN = isbn;
}
void showPublisher()
{
cout << left << setw(10) << left << setw(12);
cout << "Publisher: " << publisher << endl;
}
void setPublisher(string pub)
{
publisher = pub;
}
void showPrice()
{
cout << left << setw(10) << left << setw(12);
cout << fixed << showpoint << setprecision(2);
cout.imbue(locale("American"));
cout << showbase;
cout << "Price: " << put_money(price*100) << endl;
}
void setPrice(double amount)
{
price = amount;
}
void showYear()
{
cout << left << setw(10) << left << setw(12);
cout.imbue(locale());
cout << "Year: " << year << endl;
}
void setYear(int yearPub)
{
year = yearPub;
}
void showAuthor(int numAuthors)
{
cout << left << setw(10) << left << setw(4);
if (numAuthors == 4)
{
cout << "Author(s): " << author1 << endl;
cout << "Author(s): " << author2 << endl;
cout << "Author(s): " << author3 << endl;
cout << "Author(s): " << author4 << endl;
}
else if (numAuthors == 3)
{
cout << "Author(s): " << author1 << endl;
cout << "Author(s): " << author2 << endl;
cout << "Author(s): " << author3 << endl;
}
else if (numAuthors == 2)
{
cout << "Author(s): " << author1 << endl;
cout << "Author(s): " << author2 << endl;
}
else
{
cout << "Author(s): " << author1 << endl;
}
}
void setAuthor(string auth1, string auth2, string auth3, string auth4)
{
author1 = auth1;
author2 = auth2;
author3 = auth3;
author4 = auth4;
}
void showInStock()
{
cout << left << setw(10) << left << setw(12);
cout << "In Stock: " << inStock << endl;
}
void setInStock(int copies)
{
inStock = copies;
}
void setNumAuthors(int numbAuthors)
{
numAuthors = numbAuthors;
}
int getNumAuthors()
{
return numAuthors;
}
Book_Class()
{
title = "";
ISBN = "";
publisher = "";
price = 0.0;
year = 0;
author1 = "";
author2 = "";
author3 = "";
author4 = "";
inStock = 0;
numAuthors = 0;
}
Book_Class(string name, string number, string pub, double amount, int yearPub, string auth1, string auth2, string auth3, string auth4, int copies, int numOfAuth)
{
title = name;
ISBN = number;
publisher = pub;
price = amount;
year = yearPub;
author1 = auth1;
author2 = auth2;
author3 = auth3;
author4 = auth4;
inStock = copies;
numAuthors = numOfAuth;
}
};
Here is Books.txt file
5
C++Programing: From Problem Analysis to Program Design
5-17-525281-3
ABC
2000
52.50
20
1
Malik, D.S.
Fuzzy Discrete Structures
3-7908-1335-4
Physica-Verlag
2000
89.00
10
2
Malik, Davender
Mordeson, John
Fuzzy Mathematic in Medicine
3-7908-1325-7
Physica-Verlag
2000
89.00
10
3
Mordeson, John
Malik, Davender
Cheng, Shih-Chung
Harry John and The Magician
0-239-23635-0
McArthur A. Devine Books
1999
19.95
10
3
Goof, Goofy
Pluto, Peter
Head, Mark
Dynamic InterWeb Programming
22-99521-453-1
GNet
1998
39.99
25
1
Hemingway, Ernest
The Sun Also Rises
978-8087888155
Scribner's
1954
19.99
10
1
Hemingway, Ernest
The Old Man and the Sea
978-1781396803
Benediction Books
1952
17.99
25
1
Carroll, Lewis
Alice in Wonderland
9783959401807
MacMillan Publishers
1865
10.00
15
1
Tolkein, J.R.R.
The Fellowship of the Ring
978-0544003415
Mariner Books
1954
29.99
102
1
Tolkein, J.R.R.
The Two Towers
978-0345339713
Mariner Books
1954
19.99
54