In: Computer Science
It shows me that 1 error exists in this code but I didn't know how to fix this error so if you can help I will appreciate it.
Language C++.
Code:
#include <iostream>
#include <string>
#include <iterator>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <set>
using namespace std;
class Book
{
private:
string BookId;
string BookISBN;
string Publisher;
int PublisherYear;
double Price;
int Quantity;
string Author;
public:
string SetBookId();
string SetBookISBN();
string SetPublisher();
int SetPublisherYear();
double SetPrice();
int SetQuantity();
string SetAuthor();
string GetBookId();
string GetBookISBN();
string GetPublisher();
int GetPublisherYear();
double GetPrice();
int GetQuantity();
string GetAuthor();
void PrintInfo();
void DecrementQ();
bool isTitle(string Title);
};
string Book::GetBookId()
{
return BookId;
}
string Book::GetBookISBN()
{
return BookISBN;
}
string Book::GetPublisher()
{
return Publisher;
}
int Book::GetPublisherYear()
{
return PublisherYear;
}
double Book::GetPrice()
{
return Price;
}
int Book::GetQuantity()
{
return Quantity;
}
string Book::GetAuthor()
{
return Author;
}
string Book::SetBookId()
{
BookId=BookId;
}
string Book::SetBookISBN()
{
BookISBN=BookISBN;
}
string Book::SetPublisher()
{
Publisher=Publisher;
}
int Book::SetPublisherYear()
{
PublisherYear=PublisherYear;
}
double Book::SetPrice()
{
Price=Price;
}
int Book::SetQuantity()
{
Quantity=Quantity;
}
string Book::SetAuthor()
{
Author=Author;
}
void Book::PrintInfo()
{
cout << BookId << ":" << BookISBN << ":" << Publisher << ":" << PublisherYear << ":" << Price << ":" << Quantity << ":" << Author << endl;
}
void Book::DecrementQ()
{
if (Quantity <= 0)
{
cout << "Can't decrement since the quantity is lessthan or equal to zero" << endl;
}
else
{
Quantity= Quantity-1;
}
}
bool Book::isTitle(string Title)
{
return (BookId == Title);
}
int readDataFromFile(Book Blist[], string InFile)
{
ifstream ReadMyFile("Book.dat");
if (!ReadMyFile.is_open())
{
cout << "Error; Can't open file" << endl;
exit(1);
}
int size;
int i = -1;
string Text;
string BookId;
string BookISBN;
string Publisher;
int PublisherYear;
double Price;
int Quantity;
string Author;
getline(ReadMyFile, Text);
while (getline(ReadMyFile, Text))
{
i += 1;
Blist[i].SetBookId();
Blist[i].SetBookISBN();
Blist[i].SetPublisher();
Blist[i].SetPublisherYear();
Blist[i].SetPrice();
Blist[i].SetQuantity();
Blist[i].SetAuthor();
}
ReadMyFile.close();
size = i + 1;
return size;
}
void Printlist(Book Blist[], int size)
{
for (int i = 0; i < size; i++)
{
Blist[i].PrintInfo();
}
}
void CheckBook(Book Blist[], int size)
{
cout << "Please enter the title of the book" << endl;
string title;
cin >> title;
bool BookAvailablity = false;
for (int i = 0; i < size; i++)
{
if (Blist[i].isTitle(title) == true)
{
BookAvailablity = true;
break;
}
}
if (BookAvailablity)
{
cout << "The book that your looking for is available" << endl;
}
else
{
cout << "The book that your looking for is not available" << endl;
}
}
void UpdateNumber(Book Blist[], int size)
{
int num;
cout << "Enter a number to increment the book list" << endl;
cin >> num;
for (int i = 0; i < size; i++)
{
Blist[i].SetQuantity(GetQuantity() + num);
}
cout << "The book list is updated her is the new value in the book list" << endl;
Printlist(Blist, size);
}
int main()
{
Book Blist[10];
int size = readDataFromFile(Blist, "infile");
int c = 0;
do
{
cout << "Please enter a number from the following list" << endl;
cout << "1 - To print the list of books available in the AUD library" << endl;
cout << "2 - To check whether the book that your looking for is available in AUD library" << endl;
cout << "3 - To get the updated number of copies of the book that your looking for" << endl;
cout << "4 - To Exit the program" << endl;
cin >> c;
switch (c)
{
case 1:
Printlist(Blist, size);
break;
case 2:
CheckBook(Blist, size);
break;
case 3:
UpdateNumber(Blist, size);
break;
case 4:
cout << "Exiting Program. Thank You" << endl;
break;
default:
cout << "Error; Invalid Choice" << endl;
}
} while (c != 4);
return 0;
}
Hi Please find the updated code -
Only UpdateNumber function need to be change.
Blist[i].SetQuantity(GetQuantity() + num); -- This line need to be change.
Because GetQuantity will run only with Blist[i] and without any argument.
Book:GetQuantity will not take any argument and will get the current value of quantity to Quantity variable.
same as
Book:SetQuantity will not take any argument and will append the current value of quantity to Quantity variable.
please find the updated code -
#include <iostream>
#include <string>
#include <iterator>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <set>
using namespace std;
class Book
{
private:
string BookId;
string BookISBN;
string Publisher;
int PublisherYear;
double Price;
int Quantity;
string Author;
public:
string SetBookId();
string SetBookISBN();
string SetPublisher();
int SetPublisherYear();
double SetPrice();
int SetQuantity();
string SetAuthor();
string GetBookId();
string GetBookISBN();
string GetPublisher();
int GetPublisherYear();
double GetPrice();
int GetQuantity();
string GetAuthor();
void PrintInfo();
void DecrementQ();
bool isTitle(string Title);
};
string Book::GetBookId()
{
return BookId;
}
string Book::GetBookISBN()
{
return BookISBN;
}
string Book::GetPublisher()
{
return Publisher;
}
int Book::GetPublisherYear()
{
return PublisherYear;
}
double Book::GetPrice()
{
return Price;
}
int Book::GetQuantity()
{
return Quantity;
}
string Book::GetAuthor()
{
return Author;
}
string Book::SetBookId()
{
BookId=BookId;
}
string Book::SetBookISBN()
{
BookISBN=BookISBN;
}
string Book::SetPublisher()
{
Publisher=Publisher;
}
int Book::SetPublisherYear()
{
PublisherYear=PublisherYear;
}
double Book::SetPrice()
{
Price=Price;
}
int Book::SetQuantity()
{
Quantity=Quantity;
}
string Book::SetAuthor()
{
Author=Author;
}
void Book::PrintInfo()
{
cout << BookId << ":" << BookISBN << ":" << Publisher << ":" << PublisherYear << ":" << Price << ":" << Quantity << ":" << Author << endl;
}
void Book::DecrementQ()
{
if (Quantity <= 0)
{
cout << "Can't decrement since the quantity is lessthan or equal to zero" << endl;
}
else
{
Quantity= Quantity-1;
}
}
bool Book::isTitle(string Title)
{
return (BookId == Title);
}
int readDataFromFile(Book Blist[], string InFile)
{
ifstream ReadMyFile("Book.dat");
if (!ReadMyFile.is_open())
{
cout << "Error; Can't open file" << endl;
exit(1);
}
int size;
int i = -1;
string Text;
string BookId;
string BookISBN;
string Publisher;
int PublisherYear;
double Price;
int Quantity;
string Author;
getline(ReadMyFile, Text);
while (getline(ReadMyFile, Text))
{
i += 1;
Blist[i].SetBookId();
Blist[i].SetBookISBN();
Blist[i].SetPublisher();
Blist[i].SetPublisherYear();
Blist[i].SetPrice();
Blist[i].SetQuantity();
Blist[i].SetAuthor();
}
ReadMyFile.close();
size = i + 1;
return size;
}
void Printlist(Book Blist[], int size)
{
for (int i = 0; i < size; i++)
{
Blist[i].PrintInfo();
}
}
void CheckBook(Book Blist[], int size)
{
cout << "Please enter the title of the book" << endl;
string title;
cin >> title;
bool BookAvailablity = false;
for (int i = 0; i < size; i++)
{
if (Blist[i].isTitle(title) == true)
{
BookAvailablity = true;
break;
}
}
if (BookAvailablity)
{
cout << "The book that your looking for is available" << endl;
}
else
{
cout << "The book that your looking for is not available" << endl;
}
}
void UpdateNumber(Book Blist[], int size)
{
int num,Quantity;
cout << "Enter a number to increment the book list" << endl;
cin >> num;
for (int i = 0; i < size; i++)
{
Quantity = Blist[i].GetQuantity() + num;
Blist[i].SetQuantity();
// Blist[i].SetQuantity(GetQuantity() + num);
}
cout << "The book list is updated her is the new value in the book list" << endl;
Printlist(Blist, size);
}
int main()
{
Book Blist[10];
int size = readDataFromFile(Blist, "infile");
int c = 0;
do
{
cout << "Please enter a number from the following list" << endl;
cout << "1 - To print the list of books available in the AUD library" << endl;
cout << "2 - To check whether the book that your looking for is available in AUD library" << endl;
cout << "3 - To get the updated number of copies of the book that your looking for" << endl;
cout << "4 - To Exit the program" << endl;
cin >> c;
switch (c)
{
case 1:
Printlist(Blist, size);
break;
case 2:
CheckBook(Blist, size);
break;
case 3:
UpdateNumber(Blist, size);
break;
case 4:
cout << "Exiting Program. Thank You" << endl;
break;
default:
cout << "Error; Invalid Choice" << endl;
}
} while (c != 4);
return 0;
}
As i dont have the data file so not able to run the code -
but error is gone -