In: Computer Science
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:
#include
#include
#include
#include
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 {
if(books[i]->isbn == newISBN)
{
return i;
}
}
return -1;
}
int getCourse(int courseNum, int dept)
{
for(int i=0; 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") iss>>deptCode>>courseNum;
else if(code=="GS")
iss>>deptCode>>courseNum>>sectionNum;
else if(code == "GB") iss>>isbn;
for(int i=0; i {
if(code=="GB" && classes[i]->book->isbn==isbn)
{
coutisbn<<" "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)
{
coutisbn<<" "title<<" ";
if(classes[i]->required)
cout<<"Required\n";
else cout<<"Optional\n";
}
if(code == "GC")
{
coutisbn<<" "title<<" ";
if(classes[i]->required)
cout<<"Required\n";
else cout<<"Optional\n";
}
}
}
}
void printAllInfo(string code, istringstream &iss)
{
if(code == "PB")
{
for(int i=0; i {
cout
if(classes[i]->course->deptCode==deptCode)
{
coutisbn<<" "title<<"\n";
}
}
}
else if(code == "PY")
{
int pmonth, pyear, checkMonth, checkYear;
int ch;
iss>>checkMonth>>ch>>checkYear;
for(int i=0; i {
istringstream isstreamer(books[i]->publicationDate);
isstreamer>>pmonth>>ch>>pyear;
if((pyear==checkYear &&
pmonth>checkMonth)||(pyear>checkYear))
{
cout
I have improved code as much as I could but there is no switch block as mentioned in question.
Here is code after fixing several issues but still incomplete. Please share full code.
#include
#include
#include
#include
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, string title, long isbn, int
deptCode, int sectionNum, int courseNum, istringstream
&iss)
{
/*
if(code == "GC")
iss>>deptCode>>courseNum;
else
if(code=="GS")
iss>>deptCode>>courseNum>>sectionNum;
else
if(code == "GB")
iss>>isbn;
*/
for(int i=0; i < classCounter; i++)
{
if(code=="GB" && classes[i]->book->isbn==isbn)
{
cout <<isbn<<" "<<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 << isbn << " " << title<<" ";
if(classes[i]->required)
cout<<"Required\n";
else
cout<<"Optional\n";
}
if(code == "GC")
{
cout << isbn<<" " << title<<" ";
if(classes[i]->required)
cout<<"Required\n";
else
cout<<"Optional\n";
}
}
}
}
}
void printAllInfo(string code, istringstream &iss)
{
if(code == "PB")
{
for(int i=0; i < classCounter; i++)
{
cout
if(classes[i]->course->deptCode==deptCode)
{
cout << isbn<<" " << title<<"\n";
}
}
}
else if(code == "PY")
{
int pmonth, pyear, checkMonth, checkYear;
int ch;
iss>>checkMonth>>ch>>checkYear;
for(int i=0; i < classCounter; i++){
istringstream isstreamer(books[i]->publicationDate);
isstreamer>>pmonth>>ch>>pyear;
if((pyear==checkYear &&
pmonth>checkMonth)||(pyear>checkYear))
{
//cout
}
}
}
}