Question

In: Computer Science

It shows me that 1 error exists in this code but I didn't know how to...

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;

}

Solutions

Expert Solution

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 -


Related Solutions

I didn't know , how to write this code // This file is part of www.nand2tetris.org...
I didn't know , how to write this code // This file is part of www.nand2tetris.org // and the book "The Elements of Computing Systems" // by Nisan and Schocken, MIT Press. // File name: projects/04/Fill.asm // Runs an infinite loop that listens to the keyboard input. // When a key is pressed (any key), the program blackens the screen, // i.e. writes "black" in every pixel; // the screen should remain fully black as long as the key is...
Hello I have this error in the code, I do not know how to fix it....
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string bus.h =========== #pragma once #include using namespace std; class Bus { private:    string BusId; // bus ID    string Manufacturer; // manufacturer of the bus    int BusCapacity; // bus capacity    int Mileage; // mileage of bus    char Status; // current status...
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
There is no data associated with these questions, they are just general questions. I didn't know...
There is no data associated with these questions, they are just general questions. I didn't know what is the difference between them. a. What does the population distribution count? b. What does the sample distribution count? c. What does the distribution of sample means count?
this is my Matlab code for lemur and plant population simulation. But i didn't get the...
this is my Matlab code for lemur and plant population simulation. But i didn't get the right graph. R(1,1)=35; % Initial pop of males R(2,1)=35; % Initial pop of females P(3,1)=200000; % Iniitial pop of plants r(1)=1.01; % Growth rate of male pop r(2)=1.01; % Growth rate of female pop r(3)=1.10; % Growth rate of plants K(1)=800; % Carrying capacity for male pop K(2)=600; % carrying capacity for female pop K(3)=7500000; % carrying capacity for plants    E=65; % Plants...
how do I know how much voltage the "battery" is able to provide me
how do I know how much voltage the "battery" is able to provide me
--Know how to nest subqueries: in, not in, some, all, and exists. Know how to nest...
--Know how to nest subqueries: in, not in, some, all, and exists. Know how to nest queries in the from clause. Be able to use the with clause. Be able to use scalar subqueries. --Know how to use correlation variables in subqueries. --Know how to use case in queries. --Know how to insert, delete, and update using subqueries. Study guide please give examples
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method...
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method is not static in class StaticInitializationBlock, please define the main method as:    public static void main(String[] args) This is what I'm working on class A { static int i; static { System.out.println(1); i = 100; } } public class StaticInitializationBlock { static { System.out.println(2); } public static void main(String[] args) { System.out.println(3); System.out.println(A.i); } }
COMPUTER CALCULATIONS: I need to know how to code in R for the solutions, not by...
COMPUTER CALCULATIONS: I need to know how to code in R for the solutions, not by hand. 2. Look at the data in Table 7.18 on page 368 of the textbook. These data are also given in the SAS code labeled “SAS_basketball_goal_data” and R code labeled basketball goal data . The dependent variable is goals and the independent variable is height of basketball players. Complete a SAS /R program and answer the following questions about the data set: (a) Does...
Please, explain to me how can I know when I need to use z- table and...
Please, explain to me how can I know when I need to use z- table and when to use t-table distribution. Please, be as detailed as you can. I want to look a problems and know which one I need.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT