Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Error Messages using Visual Studio 2017 I am getting the following error messages coming from the BookMain.obj all of them point to line 1.

C++ Error Messages using Visual Studio 2017 I am getting the following error messages coming from the BookMain.obj all of them point to line 1. Assignment instructions and Code appear below the error messages.

"public: void __thiscall BookClass::IncreaseDecreaseCopies(int)" (?IncreaseDecreaseCopies@BookClass@@QAEXH@Z) already defined in LibraryMain.obj

"public: void __thiscall BookClass::displayBookInfo(void)" (?displayBookInfo@BookClass@@QAEXXZ) already defined in LibraryMain.obj

"public: int __thiscall BookClass::getCopiesLeft(void)" (?getCopiesLeft@BookClass@@QAEHXZ) already defined in LibraryMain.obj

"public: class std::basic_string,class std::allocator > __thiscall BookClass::getTitle(void)" (?getTitle@BookClass@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in LibraryMain.obj

"public: void __thiscall BookClass::storeBook(class std::basic_string,class std::allocator > * const)" (?storeBook@BookClass@@QAEXQAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in LibraryMain.obj

one or more multiply defined symbols found LibraryMain

Requirements: 1) Create LibraryMain.cpp that includes BookClass.h.

2) Declare an array of BookClass items: BookClass books[MAX_BOOKS];.

3) Use the following function void getBookInfo (BookClass &);

4) Write the following functions for BookMain.cpp

a) void getBooks (BookClass[], int &); Uses input data file books.txt to load the data structure. Uses a loop to read the data from the file. Calls storeBook to store the book information in an array location. Hint: bookList{i}.storeBook (....) where bookList is the name of the array of class items and .... is the data for the book you read in from the file.

b) voidShowBooks (BookClass[],int); Loops through all the books in the array, calling member function displayBookInfo each pass to display the book in the current array location.

c) void showTitles (BookClass[], int); Loops through all the books in the array, calling member function getTitle each pass to display the title of the book in the current array location.

d) int findBook (string, BookClass[], int);A search that loops through the book array calling member function getTitle then compares the title to the search item passed in as an input argument.

e) int getMenuChoice(); Displays the following menu and returns the users choice: 1: Displayall books 2. Display book titles 3. Find book 4. Check out 5. Check in 6. Exit program

f) Menu commands "Check In" and "Check Out" ask the user to enter the name of a book, invoke findBook to determine if the book is in the library, then if the book is in the Library (array of books), sends the location of the book in the array to either checkOutBook or returnBook to increment or decrement numInStock for the particular book.

5. BookMain.cpp program

a) Calls getBooks to load the input file into the array class structure.

b) Uses a switch statement to call getMenuChoice and process the user's choice.

c) Since you are mixing cin and getline, you will need to use cin.ignore to avoid an infinite loop situation.

BookClass.h Code

#include

#include

#include

using namespace std;

class BookClass

{

string title;

string author;

string publisher;

string isbn;

double price;

int year;

int numInStock;

public:

void IncreaseDecreaseCopies(int flag);

int getCopiesLeft();

void displayBookInfo();

string getTitle();

void storeBook(string info[]);

void checkOutBook()

{

if (numInStock > 0)

{

cout << "Checking out a book." << endl;

numInStock--;

}

else

cout << "No books in stock." << endl;

}

void returnBook()

{

cout << "Book returned successfully." << endl;

numInStock++;

}

int getNumInStock()

{

return numInStock;

}

};

LibraryMain.cpp Code

#include "BookClass.h"

#include

using namespace std;

void BookClass::IncreaseDecreaseCopies(int flag)

{ // 0 for increment and 1 for decrement

int newNumber;

if (flag == 0)

{

numInStock++;

}

if (flag == 1)

{

numInStock--;

}

}

int BookClass::getCopiesLeft()

{

return numInStock;

}

string BookClass::getTitle()

{

return title;

}

void BookClass::displayBookInfo()

{

cout << "Title: " << title << endl;

cout << "Author: " << author << endl;

cout << "Publisher: " << publisher << endl;

cout << "ISBN: " << isbn << endl;

cout << "Price: " << fixed << setprecision(2) << price << endl;

cout << "Year: " << year << endl;

cout << "Books in Stock: " << numInStock << endl;

}

void BookClass::storeBook(string info[])

{

string::size_type sz;

title = info[0];

author = info[1];

publisher = info[2];

isbn = info[3];

price = stof(info[4], &sz);

year = stoi(info[5], &sz);

numInStock = stoi(info[6], &sz);

}

BookMain.cpp Code

#include

#include

#include

#include

#include

using namespace std;

#include "LibraryMain.cpp"

#define MAX_BOOKS 3

void getBooks(BookClass *books, int &totalBooks)

{

string fileName, line, tempInfo[7];

int info;

totalBooks = 0;

cout << "Enter the file name to load book information (with extension): ";

cin >> fileName;

ifstream file(fileName.c_str());

if (file.is_open())

{

info = 0;

while (getline(file, line))

{

if (line != "")

{

tempInfo[info++] = line;

if (info == 7)

{

books[totalBooks++].storeBook(tempInfo);

info = 0;

tempInfo->clear();

}

}

}

}

}

void showBooks(BookClass *books, int totalBooks)

{

for (int i = 0; i < totalBooks; i++)

{

books[i].displayBookInfo();

}

}

void showTitles(BookClass *books, int totalBooks)

{

for (int i = 0; i < totalBooks; i++)

{

cout << books[i].getTitle() << endl;

}

}

int findBook(string book, BookClass *books, int totalBooks)

{

for (int i = 0; i < totalBooks; i++)

{

if (strcmp(books[i].getTitle().c_str(), book.c_str()) == 0)

{

return i;

}

}

return -1;

}

int getMenuChoice()

{

int choice;

cout << "i. 1: Display all books ii. 2: Display book titles iii. 3 : Find book iv. 4 : Check out v. 5 : Check in vi. 6 : Exit program" << endl;

cin >> choice;

return choice;

}

int main()

{

BookClass books[MAX_BOOKS];

string book;

bool exit = false;

int totalBooks, idx;

getBooks(books, totalBooks);

while (!exit)

{

switch (getMenuChoice())

{

case 1:

showBooks(books, totalBooks);

break;

case 2:

showTitles(books, totalBooks);

break;

case 3:

cout << "enter the name of the book to find : ";

getline(cin, book);

getline(cin, book);

idx = findBook(book, books, totalBooks);

if (idx == -1)

{

cout << "This book is not available ";

}

else

{

cout << books[idx].getCopiesLeft() << " copies of " << books[idx].getTitle() << " are in stock " << endl;

}

break;

case 4:

cout << "Enter title of book to check out ";

getline(cin, book);

getline(cin, book);

idx = findBook(book, books, totalBooks);

if (idx == -1)

{

cout << "Sorry we do not stock that book ";

}

else

{

cout << "Book is available to check out ";

books[idx].IncreaseDecreaseCopies(1);

cout << books[idx].getCopiesLeft() << " copies of " << books[idx].getTitle() << " are in stock after checkout" << endl;

}

break;

case 5:

cout << "Enter tittle of book to check in ";

getline(cin, book);

getline(cin, book);

idx = findBook(book, books, totalBooks);

if (idx == -1)

{

cout << "Sorry we do not stock that book ";

}

else

{

cout << books[idx].getTitle() << " returned ";

books[idx].IncreaseDecreaseCopies(0);

}

break;

case 6:

return 0;

default:

cout << "Enter correct choice : ";

}

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions