Ok, so I've finished the "library" problem. The program takes in input and sorts the information. However the program crashes if I enter an authors name beginning with the letter z. Also if the book starts with the letter Z the program crashes. Everything else works perfectly.....e xcept for using the letter Z.
Any ideas?
Thanks,
J
Any ideas?
Code:
#include <iostream>
#include <ctype.h>
using namespace std;
struct book
{
char title[25],author[25];
int ISBN,pages,year;
};
class shelf
{
private:
book books[50];
int num_books;
public:
shelf();
~shelf(){};
void set_range();
void input(char title[]);
void output();
void lib_output();
void single_output(char title[]);
char beg_alpha[2];
char end_alpha[2];
bool full();
};
shelf::shelf()
{
int i=0;
for(i=0;i<49;++i)
{
books[i].title[i]='-';
books[i].author[i]='-';
books[i].ISBN=0;
books[i].pages=0;
books[i].year=0000;
}
for (i=0;i<2;++i)
{
beg_alpha[i]='-';
end_alpha[i]='-';
}
num_books=0;
};
bool shelf::full()
{
return (num_books==49);
}
void shelf::input(char title[])
{
strcpy (books[num_books].title,title);
if (!full())
{
cout<<"Enter the authors name."<<endl;
cin>>books[num_books].author;
cout<<"The title of the book is:"<<endl;
cout<<"Enter the ISBN of the book."<<endl;
cin>>books[num_books].ISBN;
cout<<"Enter the number of pages."<<endl;
cin>>books[num_books].pages;
cout<<"Enter the year the book was published."<<endl;
cin>>books[num_books].year;
++num_books;
}
else if (full())
cout<<"Shelf is full!"<<endl<<endl;
}
void shelf::output()
{
for (int i=0;i<num_books;++i)
{
cout<<"Author: "<<books[i].author<<endl;
cout<<"Title: "<<books[i].title<<endl;
cout<<"ISBN: "<<books[i].ISBN<<endl;
cout<<"Pages: "<<books[i].pages<<endl;
cout<<"Published "<<books[i].year<<endl<<endl;
}
}
void shelf::single_output(char title[])
{
int i=0;
for (i=0;i<num_books;++i)
{
if(!strcmp(books[i].title,title))
{
cout<<"Book has been found!"<<endl<<endl;
cout<<"Author: "<<books[i].author<<endl;
cout<<"Title: "<<books[i].title<<endl;
cout<<"ISBN: "<<books[i].ISBN<<endl;
cout<<"Pages: "<<books[i].pages<<endl;
cout<<"Published "<<books[i].year<<endl<<endl;
i=num_books+1;
}
else
cout<<endl<<"No book found!!!."<<endl<<endl;
}
}
void main()
{
char ans, title[25]={'-'}, let;
int i=0, sel=0;
shelf library[25];
cout<<"Shelf Setup:"<<endl;
for (i=0;i<24;++i)
{
library[i].beg_alpha[0]=65+i;
library[i].beg_alpha[1]='a';
library[i].end_alpha[0]=65+i;
library[i].end_alpha[1]='z';
cout<<library[i].beg_alpha[0]<<library[i].beg_alpha[1]<<"-"<<library[i].end_alpha[0]<<library[i].end_alpha[1]<<endl;
}
library[24].beg_alpha[24]='Y';
library[24].end_alpha[24]='Z';
cout<<library[24].beg_alpha[24]<<"-"<<library[24].end_alpha[24]<<endl;
do
{
cout<<"What would you like to do?"<<endl<<"Enter 1 to enter a book."<<endl<<"Enter 2 to display a SINGLE book."<<endl;
cout<<"Enter 3 to display an entire shelf."<<endl<<"Enter 4 to display the entire library."<<endl<<"Enter 5 to quit"<<endl;
cin>>sel;
do
{
switch(sel)
{
case 1:
cout<<"Enter the title of your book."<<endl;
cin>>title;
library[title[0]-65].input(title);
break;
case 2:
cout<<"Enter the name of the book you would like to display."<<endl;
cin>>title;
library[title[0]-65].single_output(title);
break;
case 3:
cout<<"Enter the first letter of the shelf you would like to dislplay."<<endl;
cin>>let;
let=toupper(let);
library[let-65].output();
break;
case 4:
cout<<"Displaying the entire library....."<<endl;
for (i=0;i<25;++i)
{
library[i].output();
}
break;
case 5:
cout<<"Exiting program.."<<endl;
break;
default:
cout<<"Make a selection from the menu."<<endl;
break;
}
}
while (sel<1||sel>5);
}
while (sel!=5);
}
J
Comment