In: Computer Science
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;
}
}
}
}
#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!!