1. #1
    Registered User
    Join Date
    Feb 2008


    I have a shell of a program and I am suppose to input the rest of the functions, though I am trying to figure out if I am doing this correctly. I have inputed one of the functions but it doesn't seem to work. Can anyone let me know if it seems like i Am doing it correctly....

     #include <iostream>
    #include <iomanip>
    using namespace std;
    /* Struct to define a book */
    struct BOOK_STRUCT { char title[40], 
                         char author[40], 
                         char isdn[10];
    /*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 row;
         cout<<setw(20)<< "title"
             <<setw(20)<< "author"
             for(row = 0; row < lib_size; row; ++row)
                     cout<<setw(20) << library[row].title
                         <<setw(20) << author[row].author;
    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}};
        cout << setprecision(2)
             << setiosflags(ios::fixed)
             << setiosflags(ios::showpoint);
        // Output message to user           
        cout<<"Welcome to Fraser Library"<<endl;
        // Loop until the user selects exit.
           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;
             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;

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    You seem to be missing any way to determine whether a book is checked out. Was your book struct given to you? 'Cause I would expect that information to be in that struct.

  4. #4
    Join Date
    Dec 2007
    Good choice of books

    Anyway, you're going to have to be more specific about it not working. What is it doing, versus what you're expecting?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > for(row = 0; row < lib_size; row; ++row)
    Try deleting the "row;"
    Also, print a newline inside this for loop.

    Seems pretty good otherwise.

    I'll save the "use std::string thing" for later.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM