// This is my solution to Assignment 5 using arrays- Kent #include #include #include #include #include class book { public: // these functions are used to get the data from the class int get_pages() { return(pages);} float get_cost() { return(cost);} char * get_title() { return(title);} // these functions are used to store the data into the class void set_pages(int p) { pages = p;} void set_cost(float f) { cost = f;} void set_title(char *); book() { pages=0; cost = 0; title = NULL;} ~book() {if(title != NULL) delete [] title;} private: char *title; int pages; float cost; }; // book void book::set_title(char * str) { if((title = new char [strlen(str)+1]) == NULL) { // there was no space available cout << "Not enough memory for the book title" << endl; } else { (void) strcpy(title,str); } } // set_title const int MAXBOOKS=30; book Books[MAXBOOKS]; // array of book structures // function headers float cost_count(int); int page_count(int); void print_books(int); int main(int argc, char **argv) { int actual_count = 0; char yn='y'; char buffer[1024]; // buffer for title input int pg_buff; // buffer for pages input float cost_buff; // buffer for cost input while(actual_count < MAXBOOKS) { // get the data from the user cout << "Please enter the book title: "; cin >> buffer; Books[actual_count].set_title(buffer); cout << "Please enter the number of pages: "; cin >> pg_buff; Books[actual_count].set_pages(pg_buff); cout << "Please enter the cost: "; cin >> cost_buff; Books[actual_count].set_cost(cost_buff); actual_count++; cout << "do you want to enter another? (y/n) "; cin >> yn; if( (tolower(yn) != 'y')) // tolower() changes its arg to lowercase break; } // while print_books(actual_count); cout << "--------------------------" << endl; cout << "Total books: " << actual_count << endl; cout << "Total pages: " << page_count(actual_count) << endl; cout << "Total cost: " << cost_count(actual_count) << endl; return 0; } // main /* go over the array of book structures printing the contents of each one */ void print_books(int count) { for(int i = 0; i < count; i++) { cout << "book number: " << i << endl; cout << "title: " << Books[i].get_title() << endl; cout << "pages: " << Books[i].get_pages() << endl; cout << "cost: " << Books[i].get_cost() << endl; cout << endl; } // for } // print_books // Add up the total count of pages int page_count(int count) { int total_pages = 0; for(int i = 0; i < count; i++ ) total_pages += Books[i].get_pages(); return(total_pages); } // page_count // Add up the total cost of all books float cost_count(int count) { float total_cost = 0.0; for(int i = 0; i < count; i++ ) total_cost += Books[i].get_cost(); return(total_cost); } // page_count