Passing / Returning a Boolean (Help)

This is a discussion on Passing / Returning a Boolean (Help) within the C++ Programming forums, part of the General Programming Boards category; I know I'm probably doing it wrong. My intention is to have the user enter a book title. If they ...

  1. #1
    EDB
    EDB is offline
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    Question Passing / Returning a Boolean (Help)

    I know I'm probably doing it wrong. My intention is to have the user enter a book title. If they enter a title that's already in the "system", I want a while loop to allow the user to keep entering a book title until it doesn't match one in the system. (Basically, it's checking to see if the book title isn't already in use. If it is in use, the program asks for a different title before writing it down into the system)

    I thought I could pass a boolean variable to my class-member-function to use in my while-loop in main function. I'm having problems with that. Can anyone help me understand what I'm doing wrong or how I could perform this task a better way?

    Summary: I want to pass/return a Boolean variable so use in a while loop. Any suggestions, hints?

    (Note: The problem I'm referring to is in Main function - Case 1.)

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    #include <conio.h>
    
    using namespace std;
    
    //Book: Title, Author(s), publisher, ISBN, price, and Year of Publication
    class bookType
    {
          private:
                  string bookTitle;
                  string bookAuthor[4]; //Up to 4 authors.
                  string bookPublisher;
                  int bookISBN;        //10 digit number code
                  double bookPrice;
                  int bookNumCopy;
          public:
                 
                  void showTitle();   //Display title
                  void setTitle(string);    //Set the Title
                  void checkTitle(string, bool);  //check the title.
                  
                  void showAuthor();
                  void setAuthor(string);
                  void checkAuthor();
                  
                  void showPublisher();
                  void setPublisher(string);
                  void checkPublisher();
                  
                  void showISBN();
                  void setISBN(int);
                  void checkISBN();
                  
                  void showPrice();
                  void setPrice(double);
                  void checkPrice();
                  
                  void showNumCopy();   //Show number of copies
                  void setNumCopy(int);    //Set number of copies
                  void updateNumCopy(); //Update number of copies
                  void returnNumCopy(); //Display number of copies
    };
    
    //Search for book by title
    //search by ISBN, and update number of copies.
    
    void bookType::showTitle()
    {
         //Display title
         cout << bookTitle;
    }
    
    void bookType::setTitle(string title)
    {
         //set the title
         bookTitle = title;
    }
    
    void bookType::checkTitle(string chkTitle, bool search)
    {
         //Search for title
         if (chkTitle == bookTitle)
            cout << "1 Book(s) found with that title.";
            search = true;
            return search;
         else
            cout << "0 Book(s) found with that title.";
            search = false;
            return search;
    }
    
    int main()
    {
     bookType aBook[100]; //100 instances of the book class
     int choice; // for the main menu
     string title; // for setting book title.
     bool search; //Will be used for setting titles.
     //BookInt - Book instance. For incrementing.
     //for loop for check title in case 1.
     //Change while Case 1 to write over.
     
     cout << "****Book Store****" << endl
          << "1 - Set Title "     << endl
          << "2 - Search Title "  << endl;
     
     cin >> choice;
     switch(choice)
     {
        case 1:
             cout << "Enter a book title: " << endl;
             cin >> title;
             aBook[0].checkTitle(title, search);
             while(search = false)
             {
                cout << "Book Title already in use. Please enter another.";
                cout << endl;
                cin >> title;
                aBook[0].checkTitle(title, search);
             }
             aBook[0].setTitle(title);
             break;
        case 2:
             cout<<"Enter a book title to Search: ";
             cin>>title;
             for (int x = 0; x < 100; x++)
             {
               aBook[x].checkTitle(title, search);
             }
             break;
        default:
                cout << "No such option!" << endl;
    }
     
     /*
     if choice == 1
        cout<<"Enter a book title: ";
        cin>>title;
        aBook[0].setTitle(title);
     else 
          if choice == 2
    */    
     
     
     getch();   
    }
    'search' is my bool. I'm trying to pass it to a class function and have it return. The compiler has a problem with that. I'm sure I'm going about this the wrong way.

    Thank you, for looking.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    You are assigning 'false' to 'search' in the while loop, not testing for equality.
    In the future, turn up compiler warnings.

  3. #3
    EDB
    EDB is offline
    Registered User
    Join Date
    Feb 2008
    Posts
    4
    whoops. Forgot about the double = bit. Well that still doesn't solve my original problem but thanks for giving me a heads up.

    turn up compiler warnings?

    edit: By the way, I changed my Class function 'checkTitle' from Void to Bool. That seemed to have fixed it. But it's giving me a new error:
    79 BookStore_Program.cpp expected primary-expression before "else"

    Not sure what that means.

    Edit2: figured it out. Damn curly braces. I always forget them. Thanks.
    Last edited by EDB; 02-17-2008 at 08:03 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It would be easier if functions that answer Yes or No simply return a bool. There isn't much use in passing a bool to such functions.

    Code:
    bool bookType::checkTitle(const string& chkTitle)
    {
         return chkTitle == bookTitle;
    }
    Other changes:
    - the task of the function is to report if the passed string matches the stored title
    - a function should do just one thing: printing messages like "1 book(s)??? found" is a different task. It's better to leave it up to the calling function to decide what and whether to print on one or another result
    - large objects are usually passed as a const reference (if you don't want them to be modified) to avoid unnecessary copying.

    Now to use this function, you might create another function which decides whether the title user entered exists in a collection of books:
    Code:
    bool titleExists(const string& title, bookType books[], int bookCount)
    {
        for (int i = 0; i < bookCount; ++i) {
            if (books[i].checkTitle(title)) return true;
        }
        return false;
    }
    And now you can use this for the higher level logic:
    Code:
        case 1:
             cout << "Enter a book title: " << endl;
             cin >> title;
             while(titleExists(title, aBook, 100))
             {
                cout << "Book Title already in use. Please enter another.";
                cout << endl;
                cin >> title;
             }
             aBook[0].setTitle(title); //broken logic, you only want to store one item?
             break;
    As to the logic problem, you might need to keep count of how many books you have entered so far. Or use a vector which keeps that count for you, and lets you push_back as many bookTypes as you like (not limiting you to 100).

    I hope this gives you some ideas how to write this program.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Quote Originally Posted by EDB View Post
    turn up compiler warnings?
    Always program with the highest compiler warnings level. The following line would have given you a warning informing you of an assignment within what was supposed to be a conditional expression.

    while (search = false)

    In Visual C++, Project Menu -> Project Properties -> Configuration Properties -> C/C++ -> Warning Level (up this to level 4)

    In GCC, compile with -Wall -Wextra

    Preferably you should also add "-pedantic -ansi" on gcc - if you are using it - since it will issue all warnings demanded by ISO C and C++ which, among other things, stops you from using compiler extensions.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Code:
    void bookType::checkTitle(string chkTitle, bool search)
    {
         //Search for title
         if (chkTitle == bookTitle) {
            cout << "1 Book(s) found with that title.";
            search = true;
            return search;
         }
         else {
            cout << "0 Book(s) found with that title.";
            search = false;
            return search;
         }
    }
    You seem to forget brackets.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a boolean var by reference
    By Cathalo in forum C++ Programming
    Replies: 13
    Last Post: 02-25-2009, 10:44 AM
  2. returning boolean did I do this right?
    By Zerohero11 in forum C++ Programming
    Replies: 15
    Last Post: 11-07-2005, 10:54 AM
  3. returning struct and passing as an argument
    By ronin in forum C Programming
    Replies: 4
    Last Post: 06-07-2003, 11:21 AM
  4. Passing And Returning Values
    By neo6132 in forum C Programming
    Replies: 1
    Last Post: 04-19-2003, 10:24 PM
  5. returning and passing CArrays
    By swordfish in forum Windows Programming
    Replies: 4
    Last Post: 11-01-2001, 01:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21