dldsob Posted July 10, 2009 Share Posted July 10, 2009 Can anyone please help me complete the definition of BOOK STRUCT and implement the three functions in order for the program to work? Thank You. #include <iostream> using namespace std; /* Struct to define a book */ struct BOOK_STRUCT { }; /*Function Name: check_out_book Parameters: A book Purpose: Function checks out a book. Nothing is returned. */ void check_out_book (BOOK_STRUCT* book) { } /*Function Name: get_book_by_title Parameters: A library of books, a title, and the size of the library Purpose: Function returns a pointer to a book based on the title supplied by the user. If the book is not found, a null pointer is returned. */ BOOK_STRUCT* get_book_by_title (BOOK_STRUCT library[], char* title, int lib_size) { } /*Function Name: print_avail_books Parameters: A library of books and the size of the library Purpose: Function goes through the library and prints only those books which are not checked out. */ void print_avail_books (BOOK_STRUCT library[], int lib_size) { } int main() { char in_title[81]; // buffer to contain input from user BOOK_STRUCT* book; // a book pointer int choice=0; // used by the menu int NUM_BOOKS = 2; // number of books in the library // library data structure BOOK_STRUCT library[] = {{"Gods and Generals", "Jeff Shaara", "123", 0},{"Killer Angels", "Michael Shaara", "143", 0}}; // Output message to user cout<<"Welcome to Fraser Library"<<endl; // Loop until the user selects exit. do{ cout<<"Please select from the following options."<<endl; cout<<"1. Search for a book."<<endl; cout<<"2. Print available books."<<endl; cout<<"3. Checkout a book."<<endl; cout<<"4. Exit"<<endl; cout<<" > "; cin>>choice; //read input if(cin.good() && choice > 0 && choice < 5){ // Make sure choice is correct if(choice == 1 || choice == 3){ // selections require title of the book cin.ignore(); // need to ignore the character return from the previous cin cout<<"Please enter the title of the book."<<endl<<" > "; cin.getline(in_title,81); //read input from user book = get_book_by_title(library, in_title, NUM_BOOKS); // get book if (book == NULL) // The book is not in the library cout<<endl<<in_title<<" not found. Please try another title"<<endl<<endl; else if (book->checked_out == true) // The book is available but checked out cout<<endl<<"Sorry, "<<book->title<<" is checked out. Please try another title"<<endl<<endl; else if (choice == 1) // The book is available and it will be checked out cout<<endl<<book->title<<" by "<<book->author<<" is available."<<endl<<endl; else{ // The book is available cout<<endl<<book->title<<" is available. I'll check it out for you now."<<endl<<endl; check_out_book(book); } } else if(choice == 2){ // user wants to print the list of books print_avail_books(library, NUM_BOOKS); } else{ //user selected exit cout<<"Thanks for coming!"<<endl; return 0; } } else { //user provided something we can't understand cout<<endl<<"Sorry, I don't understand what you selected. Please try again"<<endl<<endl; choice = 1; // This just makes sure the user gets a chance make another selection } } while(choice < 4); return 0; } Link to comment Share on other sites More sharing options...
0 Laurë Veteran Posted July 10, 2009 Veteran Share Posted July 10, 2009 You are so lazy you can't even be bothered to tell us what your homework is meant to do, never mind actually trying to do it for yourself.. :rolleyes: A struct is pretty simple. Do some reading. As for your other functions, do some reading, then write some comments - plan what they are supposed to do, step by step, then make it happen, line by line, testing each line as you go. Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted July 11, 2009 Veteran Share Posted July 11, 2009 Some pointers (you didn't expect anyone to do your homework for you right?) : - To figure out what should be in struct BOOK_STRUCT, look at what fields are accessed from any variable of type BOOK_STRUCT or BOOK_STRUCT*. For instance, in function main(), we see that book->title is accessed, where book is a BOOK_STRUCT*. You should be able to infer that BOOK_STRUCT must contain a field named "title". You should also be able to figure out the type of title; is it a number, a flag, text, etc.? - Implementing the functions boils to down to careful reading of their signature and preceding comments. For instance, let's analyse BOOK_STRUCT* get_book_by_title (BOOK_STRUCT library[], char* title, int lib_size) This functions returns a BOOK_STRUCT*, representing a book, based on its title, which is a char*. You will have to look in library which is an array of BOOK_STRUCTs, and the variable lib_size represents the total size of the library, allowing you to know where to stop. The central operation in this function should be a comparison between titles, and you will have to use a loop to go over the entire library. I hope this helps get you started. If you have any more specific question, please ask. Link to comment Share on other sites More sharing options...
0 Kalint Posted July 11, 2009 Share Posted July 11, 2009 Can anyone please help me complete the definition of BOOK STRUCT and implement the three functions in order for the program to work? Thank You.#include <iostream> using namespace std; /* Struct to define a book */ struct BOOK_STRUCT { }; /*Function Name: check_out_book Parameters: A book Purpose: Function checks out a book. Nothing is returned. */ void check_out_book (BOOK_STRUCT* book) { } /*Function Name: get_book_by_title Parameters: A library of books, a title, and the size of the library Purpose: Function returns a pointer to a book based on the title supplied by the user. If the book is not found, a null pointer is returned. */ BOOK_STRUCT* get_book_by_title (BOOK_STRUCT library[], char* title, int lib_size) { } /*Function Name: print_avail_books Parameters: A library of books and the size of the library Purpose: Function goes through the library and prints only those books which are not checked out. */ void print_avail_books (BOOK_STRUCT library[], int lib_size) { } int main() { char in_title[81]; // buffer to contain input from user BOOK_STRUCT* book; // a book pointer int choice=0; // used by the menu int NUM_BOOKS = 2; // number of books in the library // library data structure BOOK_STRUCT library[] = {{"Gods and Generals", "Jeff Shaara", "123", 0},{"Killer Angels", "Michael Shaara", "143", 0}}; // Output message to user cout<<"Welcome to Fraser Library"<<endl; // Loop until the user selects exit. do{ cout<<"Please select from the following options."<<endl; cout<<"1. Search for a book."<<endl; cout<<"2. Print available books."<<endl; cout<<"3. Checkout a book."<<endl; cout<<"4. Exit"<<endl; cout<<" > "; cin>>choice; //read input if(cin.good() && choice > 0 && choice < 5){ // Make sure choice is correct if(choice == 1 || choice == 3){ // selections require title of the book cin.ignore(); // need to ignore the character return from the previous cin cout<<"Please enter the title of the book."<<endl<<" > "; cin.getline(in_title,81); //read input from user book = get_book_by_title(library, in_title, NUM_BOOKS); // get book if (book == NULL) // The book is not in the library cout<<endl<<in_title<<" not found. Please try another title"<<endl<<endl; else if (book->checked_out == true) // The book is available but checked out cout<<endl<<"Sorry, "<<book->title<<" is checked out. Please try another title"<<endl<<endl; else if (choice == 1) // The book is available and it will be checked out cout<<endl<<book->title<<" by "<<book->author<<" is available."<<endl<<endl; else{ // The book is available cout<<endl<<book->title<<" is available. I'll check it out for you now."<<endl<<endl; check_out_book(book); } } else if(choice == 2){ // user wants to print the list of books print_avail_books(library, NUM_BOOKS); } else{ //user selected exit cout<<"Thanks for coming!"<<endl; return 0; } } else { //user provided something we can't understand cout<<endl<<"Sorry, I don't understand what you selected. Please try again"<<endl<<endl; choice = 1; // This just makes sure the user gets a chance make another selection } } while(choice < 4); return 0; } Link to comment Share on other sites More sharing options...
Question
dldsob
Can anyone please help me complete the definition of BOOK STRUCT and implement the three functions in order for the program to work? Thank You.
#include <iostream>
using namespace std;
/* Struct to define a book */
struct BOOK_STRUCT
{
};
/*Function Name: check_out_book
Parameters: A book
Purpose: Function checks out a book. Nothing is returned.
*/
void check_out_book (BOOK_STRUCT* book)
{
}
/*Function Name: get_book_by_title
Parameters: A library of books, a title, and the size of the library
Purpose: Function returns a pointer to a book based on the title supplied
by the user. If the book is not found, a null pointer is returned.
*/
BOOK_STRUCT* get_book_by_title (BOOK_STRUCT library[], char* title, int lib_size)
{
}
/*Function Name: print_avail_books
Parameters: A library of books and the size of the library
Purpose: Function goes through the library and prints only those books
which are not checked out.
*/
void print_avail_books (BOOK_STRUCT library[], int lib_size)
{
}
int main()
{
char in_title[81]; // buffer to contain input from user
BOOK_STRUCT* book; // a book pointer
int choice=0; // used by the menu
int NUM_BOOKS = 2; // number of books in the library
// library data structure
BOOK_STRUCT library[] = {{"Gods and Generals", "Jeff Shaara", "123", 0},{"Killer Angels",
"Michael Shaara", "143", 0}};
// Output message to user
cout<<"Welcome to Fraser Library"<<endl;
// Loop until the user selects exit.
do{
cout<<"Please select from the following options."<<endl;
cout<<"1. Search for a book."<<endl;
cout<<"2. Print available books."<<endl;
cout<<"3. Checkout a book."<<endl;
cout<<"4. Exit"<<endl;
cout<<" > ";
cin>>choice; //read input
if(cin.good() && choice > 0 && choice < 5){ // Make sure choice is correct
if(choice == 1 || choice == 3){ // selections require title of the book
cin.ignore(); // need to ignore the character return from the previous cin
cout<<"Please enter the title of the book."<<endl<<" > ";
cin.getline(in_title,81); //read input from user
book = get_book_by_title(library, in_title, NUM_BOOKS); // get book
if (book == NULL) // The book is not in the library
cout<<endl<<in_title<<" not found. Please try another title"<<endl<<endl;
else if (book->checked_out == true) // The book is available but checked out
cout<<endl<<"Sorry, "<<book->title<<" is checked out. Please try another title"<<endl<<endl;
else if (choice == 1) // The book is available and it will be checked out
cout<<endl<<book->title<<" by "<<book->author<<" is available."<<endl<<endl;
else{ // The book is available
cout<<endl<<book->title<<" is available. I'll check it out for you now."<<endl<<endl;
check_out_book(book);
}
}
else if(choice == 2){ // user wants to print the list of books
print_avail_books(library, NUM_BOOKS);
}
else{ //user selected exit
cout<<"Thanks for coming!"<<endl;
return 0;
}
}
else { //user provided something we can't understand
cout<<endl<<"Sorry, I don't understand what you selected. Please try again"<<endl<<endl;
choice = 1; // This just makes sure the user gets a chance make another selection
}
}
while(choice < 4);
return 0;
}
Link to comment
Share on other sites
3 answers to this question
Recommended Posts