Question

In: Computer Science

I have posted this earlier, but it seems that the code part was cutoff, therefore, this...

I have posted this earlier, but it seems that the code part was cutoff, therefore, this is re post with full code.

In the program the "case: 'C'" doesn't seem to function correctly,

We were told to write a book tracking program, and it takes inputs and once all the book info such as ISBN, date, author, course number, etc are put in then input, then we can print them out, by using the print functions that are defined in the program. Example:

the formats are as follows, when defining the book: "B-ISBN-TITLE"

So then in the terminal once the code compiles I can input: "B 1234567890123 Title of book"

and thus that book is defined, and I needed to make the code print the books info out and it's format were "GB-ISBN"

So then in the terminal after the book is defined I can type "GB 1234567890123" and the isbn and books title will be printed out that were define above.

NOW my problem is with the C which defines a course and it's format is "C-Department Code-Course Number-Name"

So if type "C CSEC 215 Name of the course" it should store it, until I use the print function "PC" which will print out the course that are defined. BUT I am getting an "abort message, out of range", and it points the error at the "case: C" switch statement in the code.

Can someone take a look at the code and help me figure out the solution,

Here is the code:

else if(code == "PY")
{
int pmonth, pyear, checkMonth, checkYear;
int ch;
iss>>checkMonth>>ch>>checkYear;

for(int i=0; i<bookCounter; i++)
{
istringstream isstreamer(books[i]->publicationDate);
isstreamer>>pmonth>>ch>>pyear;
if((pyear==checkYear && pmonth>checkMonth)||(pyear>checkYear))
{
cout<<books[i]->isbn<<" "<<books[i]->title<<"\n";
}
}
}
else if(code=="PM")
{
double average = 0, max = 0, min = 100000;
int count = 0;
int deptCode;
iss>>deptCode;
for(int i=0; i<classCounter; i++)
{
if(classes[i]->course->deptCode==deptCode)
{
average +=classes[i]->book->usedRate == -1?0:classes[i]->book->usedRate;
average +=classes[i]->book->newRate == -1?0:classes[i]->book->newRate;
average +=classes[i]->book->rentedRate == -1?0:classes[i]->book->rentedRate;
average +=classes[i]->book->eRate == -1?0:classes[i]->book->eRate;
if(classes[i]->book->usedRate != -1)
{
if(classes[i]->book->usedRate > max) max = classes[i]->book->usedRate;
if(classes[i]->book->usedRate < min && classes[i]->required) min = classes[i]->book->usedRate;
count++;
}
if(classes[i]->book->newRate != -1)
{
if(classes[i]->book->newRate > max) max = classes[i]->book->newRate;
if(classes[i]->book->newRate < min && classes[i]->required) min = classes[i]->book->newRate;
count++;
}
if(classes[i]->book->rentedRate != -1)
{
if(classes[i]->book->rentedRate > max) max = classes[i]->book->rentedRate;
if(classes[i]->book->rentedRate < min && classes[i]->required) min = classes[i]->book->rentedRate;
count++;
}
if(classes[i]->book->eRate != -1)
{
if(classes[i]->book->eRate > max) max = classes[i]->book->eRate;
if(classes[i]->book->eRate < min && classes[i]->required) min = classes[i]->book->eRate;
count++;
}
}
}
cout<<"Average: "<<average/count<<" Min Cost: "<<min<<" Max Cost: "<<max<<endl;
}
}
int main()
{
cout<<"Enter 'quit' to quit the program\n\n";
for (string line; getline(std::cin, line);) {
if(line == "quit")
exit(0);
else
{
string code;
istringstream iss(line);
iss>>code;
long newISBN = 0;
int bookIndex = 0;
char updateCode;
switch(code.at(0))
{
case 'B': books[bookCounter] = new Book;
iss>>books[bookCounter]->isbn;
books[bookCounter]->title = iss.str().substr(iss.tellg());
bookCounter++;
break;
case 'D': iss>>newISBN;
bookIndex = getBook( newISBN);
iss>>updateCode;
if(updateCode=='A')
books[bookIndex]->author = iss.str().substr(iss.tellg());
else if(updateCode=='E')
iss>>books[bookIndex]->edition;
else iss>>books[bookIndex]->publicationDate;
break;
case 'M': iss>>newISBN;
double cost;
bookIndex = getBook( newISBN);
iss>>cost>>updateCode;
if(updateCode=='N') books[bookIndex]->newRate = cost;
else if(updateCode=='U') books[bookIndex]->usedRate = cost;
else if(updateCode=='R') books[bookIndex]->rentedRate = cost;
else if(updateCode=='E') books[bookIndex]->eRate = cost;
break;
case 'C': course[courseCounter] = new Course; //Help with this case, error thrown here.
iss>>course[courseCounter]->deptCode>>course[courseCounter]->courseNumber;
course[courseCounter]->name = iss.str().substr(iss.tellg());
courseCounter++;
break;
case 'A': iss>>newISBN;
int deptCode, courseNum, courseIndex;
iss>>deptCode>>courseNum;
courseIndex = getCourse( courseNum, deptCode);
bookIndex = getBook( newISBN);
classes[classCounter] = new Class;
classes[classCounter]->book = books[bookIndex];
classes[classCounter]->course = course[courseIndex];
iss>>classes[classCounter]->sectionNumber;
iss>>classes[classCounter]->required;
classCounter++;
break;
case 'G': printBooks(code, iss); break;
case 'P': printAllInfo(code, iss); break;
}
}
}
}

Solutions

Expert Solution

#include<iostream>
#include <sstream> // std::istringstream
#include <string>
#include<stdlib.h>
using namespace std;


int bookCounter = 0, courseCounter = 0, classCounter = 0;

struct Book {
long isbn;
string title;
string author;
int edition;
string publicationDate;
double newRate = -1;
double usedRate = -1;
double rentedRate = -1;
double eRate = -1;
} * books[100];

struct Course {
int deptCode;
int courseNumber;
string name;

} *course[100];

struct Class {
struct Book *book;
struct Course *course;
int sectionNumber;
bool required;
} *classes[100];

int getBook(long newISBN) {
for (int i = 0; i < bookCounter; i++) {
if (books[i]->isbn == newISBN) {
return i;
}
}
return -1;
}

int getCourse(int courseNum, int dept) {
for (int i = 0; i < courseCounter; i++) {
if (course[i]->courseNumber == courseNum && course[i]->deptCode == dept) {
return i;
}
}
return -1;
}

void printBooks(string code, istringstream &iss) {
int deptCode, sectionNum, courseNum;
long isbn;
if (code == "GC") {
cout << "Enter Dept Code and Course name: " << endl;
iss >> deptCode >> courseNum;
} else if (code == "GS") {
cout << "Enter Dept Code, Course name and section: " << endl;
iss >> deptCode >> courseNum >> sectionNum;
} else if (code == "GB") {
cout << "Enter isbn code: " << endl;
iss >> isbn;
}
for (int i = 0; i < classCounter; i++) {
if (code == "GB" && classes[i]->book->isbn == isbn) {
cout << classes[i]->book->isbn << " " << classes[i]->book->title << " ";
if (classes[i]->required)
cout << "Required\n";
else cout << "Optional\n";
} else if (classes[i]->course->courseNumber == courseNum && classes[i]->course->deptCode == deptCode) {
if (code == "GS" && classes[i]->sectionNumber == sectionNum) {
cout << classes[i]->book->isbn << " " << classes[i]->book->title << " ";
if (classes[i]->required)
cout << "Required\n";
else cout << "Optional\n";
}
if (code == "GC") {
cout << classes[i]->book->isbn << " " << classes[i]->book->title << " ";
if (classes[i]->required)
cout << "Required\n";
else cout << "Optional\n";
}
}
}
}

void printAllInfo(string code, istringstream &iss) {
if (code == "PB") {
int deptCode;
cout << " Enter Dept Code: " << endl;
iss>>deptCode;
for (int i = 0; i < classCounter; i++) {
if (classes[i]->course->deptCode == deptCode) {
cout << classes[i]->book->isbn << " " << classes[i]->book->title << "\n";
}
}
} else if (code == "PY") {
int pmonth, pyear, checkMonth, checkYear;
int ch;
cout << "enter month, date and year : " << endl;

iss >> checkMonth >> ch>>checkYear;

for (int i = 0; i < bookCounter; i++) {
istringstream isstreamer(books[i]->publicationDate);
cout << "enter month, date and year : " << endl;
isstreamer >> pmonth >> ch>>pyear;
if ((pyear == checkYear && pmonth > checkMonth) || (pyear > checkYear)) {
cout << books[i]->isbn << " " << books[i]->title << "\n";
}
}
} else if (code == "PM") {
double average = 0, max = 0, min = 100000;
int count = 0;
int deptCode;
cout << " Enter Dept Code: " << endl;
iss>>deptCode;
for (int i = 0; i < classCounter; i++) {
if (classes[i]->course->deptCode == deptCode) {
average += classes[i]->book->usedRate == -1 ? 0 : classes[i]->book->usedRate;
average += classes[i]->book->newRate == -1 ? 0 : classes[i]->book->newRate;
average += classes[i]->book->rentedRate == -1 ? 0 : classes[i]->book->rentedRate;
average += classes[i]->book->eRate == -1 ? 0 : classes[i]->book->eRate;
if (classes[i]->book->usedRate != -1) {
if (classes[i]->book->usedRate > max) max = classes[i]->book->usedRate;
if (classes[i]->book->usedRate < min && classes[i]->required) min = classes[i]->book->usedRate;
count++;
}
if (classes[i]->book->newRate != -1) {
if (classes[i]->book->newRate > max) max = classes[i]->book->newRate;
if (classes[i]->book->newRate < min && classes[i]->required) min = classes[i]->book->newRate;
count++;
}
if (classes[i]->book->rentedRate != -1) {
if (classes[i]->book->rentedRate > max) max = classes[i]->book->rentedRate;
if (classes[i]->book->rentedRate < min && classes[i]->required) min = classes[i]->book->rentedRate;
count++;
}
if (classes[i]->book->eRate != -1) {
if (classes[i]->book->eRate > max) max = classes[i]->book->eRate;

if (classes[i]->book->eRate < min && classes[i]->required) min = classes[i]->book->eRate;
count++;
}
}
}
cout << "Average: " << average / count << " Min Cost: " << min << " Max Cost: " << max << endl;
}
}

int main() {
cout << "Enter 'quit' to quit the program\n\n";
for (string line; getline(std::cin, line);) {
if (line == "quit")
exit(0);
else {
string code;
istringstream iss(line);
cout << " Enter Code: " << endl;

iss>>code;
long newISBN = 0;
int bookIndex = 0;
char updateCode;
switch (code.at(0)) {
case 'B': books[bookCounter] = new Book;
cout << " Enter isbn Code: " << endl;
iss >> books[bookCounter]->isbn;
cout << " Enter book title: " << endl;
books[bookCounter]->title = iss.str().substr(iss.tellg());
bookCounter++;
break;
case 'D':
cout << " Enter isbn Code: " << endl;
iss>>newISBN;
bookIndex = getBook(newISBN);
cout << " Enter new Code: " << endl;
iss>>updateCode;
if (updateCode == 'A') {
cout << " Enter Author: " << endl;
books[bookIndex]->author = iss.str().substr(iss.tellg());
} else if (updateCode == 'E') {
cout << " Enter Edition: " << endl;
iss >> books[bookIndex]->edition;
} else {
cout << " Enter publicationDate: " << endl;
iss >> books[bookIndex]->publicationDate;
}
break;
case 'M':
cout << " Enter isbn Code: " << endl;
iss>>newISBN;
double cost;
bookIndex = getBook(newISBN);
cout << " Enter cost and new Code: " << endl;
iss >> cost>>updateCode;
if (updateCode == 'N') books[bookIndex]->newRate = cost;
else if (updateCode == 'U') books[bookIndex]->usedRate = cost;
else if (updateCode == 'R') books[bookIndex]->rentedRate = cost;
else if (updateCode == 'E') books[bookIndex]->eRate = cost;
break;
case 'C': course[courseCounter] = new Course; //Help with this case, error thrown here.
cout << " Enter dept Code and course number: " << endl;
iss >> course[courseCounter]->deptCode >> course[courseCounter]->courseNumber;
cout << " Enter course name: " << endl;
course[courseCounter]->name = iss.str().substr(iss.tellg());
courseCounter++;
break;
case 'A':
cout << " Enter isbn Code: " << endl;
iss >> newISBN;
int deptCode, courseNum, courseIndex;
cout << " Enter dept Code and course number: " << endl;
iss >> deptCode >> courseNum;
courseIndex = getCourse(courseNum, deptCode);
bookIndex = getBook(newISBN);
classes[classCounter] = new Class;
classes[classCounter]->book = books[bookIndex];
classes[classCounter]->course = course[courseIndex];
cout << " Enter section Number: " << endl;
iss >> classes[classCounter]->sectionNumber;
cout << " Enter required: " << endl;
iss >> classes[classCounter]->required;
classCounter++;
break;
case 'G': printBooks(code, iss);
break;
case 'P': printAllInfo(code, iss);
break;
}
}
}
}

i have rectified most of the errors of your code and you need to first add the book then the course and then class so that your program works fine as all are linked

Thanks!

Good Luck!!


Related Solutions

I posted this question earlier but I don't quite understand the development for part b) and...
I posted this question earlier but I don't quite understand the development for part b) and c). I need some details because it's really not obvious for me. Part a) is good. The question is : We start the operation of a reversible Carnot Engine between two reservoir of temperature T1 and T2 where T2 > T1. The colder reservoir is so cold that his temperature doesn't change during the process of the engine. The hotter reservoir is composed of...
How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
Write code in SAS to do each of the following I have posted the data below...
Write code in SAS to do each of the following I have posted the data below from a pace delimited data set consisting of 66 randomly selected cars Upload the data set to SAS and store it as a SAS data set called cars. Make sure the full values are stored for all character variables. Create a comparative bar chart (with appropriate title and labels) displaying the brands of each car by fuel type (so that fuel type is on...
I am trying to do edge detection using matlab. I have posted code here. It does...
I am trying to do edge detection using matlab. I have posted code here. It does not give any error but it's not running it at all. So please help me to fix it and also exaplin each line of this code pls. Thanks! i = imread('C:\Users\Amanda\Desktop"); I = rgb2gray(1i); BW1 = edge(I,'prewitt'); BW2= edge(I,'sobel'); BW3= edge(I,'roberts'); subplot (2,2,1); imshow(I); title('original'); subplot(2,2,2); imshow(BW1); title('Prewitt'); subplot(2,2,3); imshow(BW2); title('Sobel'); subplot(2,2,4); imshow(BW3); title('Roberts');
second part of the previous question already i have posted Income statement for Mars for 2018...
second part of the previous question already i have posted Income statement for Mars for 2018 and 2019 follows: Particulars 2019 ($) 2018 ($) Sales 250,000 180,000 Cost of Goods sold 140,000 110,000 Income before taxes 25000 25000 Income Tax expenses 4000 3000 Selling expenses 20,000 19,000 Interest expenses 3,500 4,500 Compute the ratios from the given balance sheet extract for 2018. Assets $ Cash 30,000 Marketable securities 17,000 Accounts receivables 12,000 Stock 10,000 Land and Building 200,000 Accumulated depreciation...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work. Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO. Test (Main) List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size...
I posted a question yesteraday and got an answer. There is a second part to the...
I posted a question yesteraday and got an answer. There is a second part to the question Problem 6-4AA Periodic: Alternative cost flows P3 Refer to the information in Problem 6-3A and assume the periodic inventory system is used. Required Compute cost of goods available for sale and the number of units available for sale. Compute the number of units in ending inventory. Compute the cost assigned to ending inventory using (a) FIFO, (b) LIFO, (c) weighted average, and (d)...
Dear Colleague, Earlier today I built my seventh website using HTML5, CSS3, and Bootstrap4. Bootstrap seems...
Dear Colleague, Earlier today I built my seventh website using HTML5, CSS3, and Bootstrap4. Bootstrap seems amazing, but it never did what it should do – make my website look better. Also, my page should have a blue theme that was provided in the CSS folder. This also didn’t work. My website is supposed to look like what is shown in Figure 1 (see below). Someone told me that there are exactly 10 errors on index.html, 10 errors on contact.html,...
Dear Colleague, Earlier today I built my seventh website using HTML5, CSS3, and Bootstrap4. Bootstrap seems...
Dear Colleague, Earlier today I built my seventh website using HTML5, CSS3, and Bootstrap4. Bootstrap seems amazing, but it never did what it should do – make my website look better. Also, my page should have a blue theme that was provided in the CSS folder. This also didn’t work. My website is supposed to look like what is shown in Figure 1 (see below). Someone told me that there are exactly 10 errors on index.html, 10 errors on contact.html,...
I have do calculate ratios for the company Generals Mills and I have the link posted...
I have do calculate ratios for the company Generals Mills and I have the link posted below for the financial statements from thier website. I need work shown with the answers to get full grade so please post that too. Thank you https://s22.q4cdn.com/584207745/files/doc_financials/2018/annual/FINAL-2018-Annual-Report.pdf You are the newly appointed treasurer and your partner is the newly appointed controller of your company. In order to learn more about your company, you have decided to analyze the company’s financial performance over the last...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT