Hello everyone. I'm working on a program which uses a class (Shelf) and a struct (Book). In the main program we declare an array (Library[25]) which is of type shelf. The program takes in various info about books (author, title, isbn, etc) and then allows you to search for books starting with a certain letter, and in turn displays those books and the info on them.
At this point I can enter the info, and display the info for 1 book. I'm confused about where I should look to enter more info. I'm assuming it should be in my main. Do I need to increment i (my counter) at the end of the loop so the info is not copying over itself?
Thanks,
J
At this point I can enter the info, and display the info for 1 book. I'm confused about where I should look to enter more info. I'm assuming it should be in my main. Do I need to increment i (my counter) at the end of the loop so the info is not copying over itself?
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]; public: shelf(); ~shelf(){}; void input(); void output(); char alpha[2]; bool full; }; shelf::shelf() { int i=0; cout<<"constructing our shelves"<<endl; for(i=0;i<50;++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) { alpha[i]='-'; } full=false; }; void shelf::input() { int sel=0,i=0; cout<<"This function allows the input of information for books."<<endl; cout<<"What would you like to enter?"<<endl; do { cout<<"1) Enter authors name."<<endl<<"2) Enter the title of the book."<<endl<<"3) Enter the ISBN."<<endl<<"4) Enter the number of pages."<<endl; cout<<"5) Enter the year the book was published."<<endl<<"6) Enter all of the information."<<endl; cin>>sel; switch(sel) { case 1: cout<<"Enter the authors name."<<endl; cin.getline(books[i].author,20,'/n'); cin.ignore(); break; case 2: cout<<"Enter the title of the book."<<endl; cin.getline (books[i].title,20,'/n'); cin.ignore(); break; case 3: cout<<"Enter the ISBN of the book."<<endl; cin>>books[i].ISBN; break; case 4: cout<<"Enter the number of pages."<<endl; cin>>books[i].pages; break; case 5: cout<<"Enter the year the book was published."<<endl; cin>>books[i].year; break; case 6: cout<<"Enter the authors name."<<endl; { cin.ignore(); cin.getline(books[i].author,30,'\n'); cout<<books[i].author<<endl; } cout<<"Enter the title of the book."<<endl; { cin.getline (books[i].title,30,'\n'); cout<<books[i].title<<endl; } cout<<"Enter the ISBN of the book."<<endl; cin>>books[i].ISBN; cout<<books[i].ISBN<<endl; cout<<"Enter the number of pages."<<endl; cin>>books[i].pages; cout<<books[i].pages<<endl; cout<<"Enter the year the book was published."<<endl; cin>>books[i].year; cout<<books[i].year<<endl; break; default: cout<<"Make a selection from the menu."<<endl; break; } } while (sel<1||sel>6); } void shelf::output() { int i=0; char let; cout<<"Enter the letter for which you would like to display titles: "<<endl; cin>>let; let=toupper(let); if (books[i].title[i]==let) { cout<<"Author Title ISBN #Pages Published"<<endl; cout<<books[i].author<<" "<<books[i].title<<" "<<books[i].ISBN<<" "<<books[i].pages<<" "<<books[i].year<<endl; } else cout<<"No book titles found which begin with the letter "<<let<<endl; } void main() { char ans='Y'; int i=0; shelf library[25]; do { cout<<"Do you have a book to enter? Enter Y for yes or # for no."<<endl; cin>>ans; ans=toupper(ans); library[i].input(); library[i].output(); ++i; } while (ans!='#'); // library[i].output(); cout<<"Exiting program."<<endl; }
J
Comment