Creating a Library management system in C++ and database in SQL

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3730 Views - Last Post: 04 December 2012 - 11:37 AM Rate Topic: -----

#1 flameon87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-December 12

Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 01:52 PM

#include<iostream>
#include<vector>
#include<fstream>
#include "Library.h"
#include "sqlite3.h"

void Library::ManageCheckout()
{
    control = 1;

    while(control != 4)
    {
        cout << endl << "What would you like to do?" << endl;
        cout << "1 - Show checkouts" << endl;
        cout << "2 - Check-out material" << endl;
        cout << "3 - Check-in material" << endl;
        cout << "4 - Return to main menu" << endl;
        cout << "-->";
        cin >> control;

        while(control < 1 || control > 4)
        {
            cout << "Incorrect input, try again" << endl << "-->";
            cin >> control;
        }

        switch(control)
        {
            case 1:
                PrintCheckouts();
                break;
            case 2:
                checkOut();
                break;
            case 3:
                checkIn();
                break;
            case 4:
                break;
        }
    }
}

void Library::PrintCheckouts()
{
    //Typical select statement layout, it can all go in one line if you want
    cout << sqlite3_exec(db, //<-- created in Library.cpp after libraries are included, links the program to the database
              "select * from Checkout;", //<-- Select statement goes here
              NULL, //Always put these next three arguments in when issuing an SQL command
              0,
              NULL);
}

/*void Library::BookCheckout()
{*/
    //Prompt user for data
     bool checkOut( const std::vector<ManagePatrons>& vecPatrons, const std::vector<ManageMaterials>& vecMaterials, std::vector<checkOut>& vecChecked ){
     Patron patronOut;
     Material materialOut;
     bool success = false;
     std::string strDate = "";

     // Get the patron.
     patronOut = selectPatron( vecPatrons );

     // Get the material.
     materialOut = selectMaterial( vecMaterials );

     // Get the due date.
     do{const std::vector<Patron>& vecPatrons, const std::vector<Material>& vecMaterials, std::vector<CheckedOut>& vecChecked
          std::cout << "Enter the due date (mm/dd/yyyy): ";
          cin >> strDate;

          if( strDate.length() < 10 ){
              // Alert user.
              std::cout << "Error!  Invalid date format." << endl;
          } else {
                 success = true;
          }
     } while( !success );

     // Add this item to the list of checked out items.
     vecChecked.push_back( CheckedOut( patronOut.getID(), materialOut.getTitle(), strDate ) );

     return success;
    //Enter data into Header file

    //Insert data into database
}

/*void Library::BookCheckin()
{*/
    //Ask what book was checked out
    bool checkIn( const std::vector<Patron>& vecPatrons, std::vector<CheckedOut>& vecChecked ){
     bool success = false;

       // Function variables...
       static const int LIST_SIZE = 10;
       int offset = 0;
       int selection = -1;
       char cSelect = ' ';
       bool valid = false;
       bool found = false;

       do{
              std::cout << "Make a selection from the menu below: " << endl << endl;

              // Print the specified number of entries.
              for( int menuCnt = 0; menuCnt < LIST_SIZE && menuCnt+offset < vecChecked.size(); ++menuCnt ){
                   // Print out each line selection option.
                   cout << menuCnt+1 << ".\t";

                   // Find and print the patron's name.
                   for(std::vector<Patron>::const_iterator iter_patron = vecPatrons.begin(); iter_patron < vecPatrons.end() && !found ; ++iter_patron ){
                         found = (iter_patron->getID() == vecChecked[offset+menuCnt].getPatronID() );

                         if( found ){
                             cout << iter_patron->getName().toStr();
                         }
                   }

                   std::cout << "\t" << vecChecked[offset+menuCnt].getMaterial() << "\t" << vecChecked[offset+menuCnt].getDueDate() << endl;
              }

              if( offset > 0 ){
                  std::cout << "\t\t p for previous page";
              }

              if( offset < (int) vecChecked.size() - LIST_SIZE ){
                  std::cout << "\t\t n for next page";
              }

              std::cout << endl << endl << "Enter selection: ";
              cin.ignore(1);    // Ignore previous return stroke.
              cin >> cSelect;

              switch( cSelect ){
                      case 'p':
                      case 'P':
                           if( offset > 0 ){
                                                      offset -= LIST_SIZE;
                           }
                           break;
                      case 'n':
                      case 'N':
                           if( offset < (int) vecPatrons.size() - LIST_SIZE ){
                                                      offset += LIST_SIZE;
                           }
                           break;
                      default:
                           selection = (int) cSelect - 49;
                           if( selection < 0 || selection > 9 ){
                               // Alert user.
                               std::cout << "Error!  Invalid selection attempted." << endl << endl;
                           } else {
                                  // Set flag for a valid entry.
                                  selection += offset;
                                  valid = true;
                           }
              }
       }while( !valid );

       // Remove the selected checked material from the vector.
       vecChecked.erase( vecChecked.begin() + selection );

     return success;
}


Codeblocks tells me that line 70 has impromper declarations and incompatible types. then it also says my atoi function is not properly declared. I have tried getting assistance from my instructor however she says she is lost by my code and doesn't know how to help me. I have also tried googling to look for references and help but haven't had any luck thus far.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a Library management system in C++ and database in SQL

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 02:04 PM

You're missing something at the end of this:

const std::vector<Patron>& vecPatrons, const std::vector<Material>& vecMaterials, std::vector<CheckedOut>& vecChecked


EDIT: Actually, I think that's just a copy/paste failure.

atoi is part of stdlib.h (cstdlib in C++), which you could have found out by going to Google and typing in "man atoi".
Was This Post Helpful? 0
  • +
  • -

#3 flameon87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-December 12

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 04:29 PM

However, I had it in there before and it created a ton of other errors, to which im not sure why they are there. I will post the error codes if you want, but Im just really trying to get ahead of this thing.
Was This Post Helpful? 0
  • +
  • -

#4 flameon87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-December 12

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 05:21 PM

Here are the errors that come up in code blocks with the #include<cstdlib>. I do apologize earlier for not putting up the error codes in great detail.

Line 58- error: ManagePatrons was not declared in this scope
Line 58- error: template argument 1 is invalid
Line 58- error: template argument 2 is invalid
Line 58- error: ManageMaterials was not declared in this scope
Line 58- error: template argument 1 is invalid
Line 58- error: template argument 2 is invalid
Line 58- error: checkOut was not declared in this scope
Line 58- error: template argument 1 is invalid
Line 58- error: template argument 2 is invalid
Line 60- error: Material was not declared in this scope
Line 60- error: expected ';' before 'materialOut'
Line 71- error: 'vecPatrons' declared as reference but not initialized
Line 71- error: expected unqualified-id before 'const'


These are some of the errors. I have been working on this problem for a couple weeks now. I started out working on it by myself and tried to debug and fix my logic errors but due to my limited knowledge and the want and need to become more effecient in programming I am reaching out to those who can teach me the way of the right of passage if you will.
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,225
  • Joined: 20-March 10

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 05:45 PM

Hi,

Could you please post the contents of Library.h its unclear where
you have the Patron Class etc declared in this code...

Thanks

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#6 flameon87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-December 12

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 05:50 PM

#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include "Patron.h"
#include "Book.h"
#include "Checkout.h"
#include "sqlite3.h"
using namespace std;

class Library
{
    private:
        typedef struct sqlite3 sqlite3;
        sqlite3 *db;
        int choice, control;
        //     ^       ^
        //     |       Used in the "Manage*" functions
        //    Used in "PromptUser"
        Book book;
        Patron patron;
        Checkout checkout;
        sqlite3_stmt* does_table_exist;

    public:
        Library();
        void promptUser();

        bool dbopen();
        bool Materials_Exists();
        bool Patrons_Exists();
        bool Checkout_Exists();

        bool createMaterials();
        bool createPatrons();
        bool createCheckout();

        void ManageMaterials();
        void PrintMaterial();
        void AddMaterial();
        void RemoveMaterial();

        void ManagePatrons();
        void PrintPatrons();
        void AddPatron();
        void RemovePatron();

        void ManageCheckout();
        void PrintCheckouts();
        bool checkOut();
        bool checkIn();
        void GenerateReport();
};


Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 06:03 PM

> bool checkOut( const std::vector<ManagePatrons>& vecPatrons, ...
> void ManagePatrons();

In the checkOut function, what does ManagePatrons refer to?
Was This Post Helpful? 0
  • +
  • -

#8 flameon87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-December 12

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 06:07 PM

It refers to the following code which is another file within my library management system.

void Library::ManagePatrons()
{
    control = 1;

    while(control != 4)
    {
        cout << endl << "What would you like to do?" << endl;
        cout << "1 - Show patrons" << endl;
        cout << "2 - Add patron" << endl;
        cout << "3 - Remove patron" << endl;
        cout << "4 - Return to main menu" << endl;
        cout << "-->";
        cin >> control;

        while(control < 1 || control > 4)
        {
            cout << "Incorrect input, try again" << endl << "-->";
            cin >> control;
        }

        switch(control)
        {
            case 1:
                PrintPatrons();
                break;
            case 2:
                AddPatron();
                break;
            case 3:
                RemovePatron();
                break;
            case 4:
                break;
        }
    }
}

void Library::PrintPatrons()
{
    //Typical select statement layout, it can all go in one line if you want
    cout << sqlite3_exec(db, //<-- created in Library.cpp after libraries are included, links the program to the database
              "select * from Patrons;", //<-- Select statement goes here
              NULL, //Always put these next three arguments in when issuing an SQL command
              0,
              NULL);
}

void Library::AddPatron()
{
       //Prompt user for data
       int id;                 // Patron's id number.
       std::string last;       // Patron's last name.
       std::string first;      // Patron's first name.
       std::string phone;      // Patron's phone number.
       std::string email;      // Patron's e-mail address.
       std::string street;     // Patron's street address.
       std::string city;       // Patron's city.
       std::string state;      // Patron's state.
       std::string zip;        // Patron's zip code.

       // Get the information from the user.
       cout << "Enter patron's id number: ";
       cin >> id;
       cout << "Enter patron's last name: ";
       getline( cin, last );
       cout << "Enter patron's first name: ";
       getline( cin, first );
       cout << "Enter patron's phone number: ";
       cin >> phone;
       cout << "Enter patron's e-mail address: ";
       cin >> email;
       cout << "Enter patron's street address: ";
       cin.ignore(1);
       getline( cin, street );
       cout << "Enter patron's city: ";
       getline( cin, city );
       cout << "Enter patron's state: ";
       cin >> state;
       cout << "Enter patron's zip: ";
       cin >> zip;

       return Patron(id, Name(last, first), phone, email, street, city, state, zip);
    //Enter data into Header file

    //Insert data into database
}

void Library::RemovePatron()
{
    //Ask what entry the user wants to remove
}

Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 06:10 PM

Quote

It refers to the following code which is another file within my library management system.
So, to summarize, ManagePatrons is a member function of class Library?

Let's go back to the code you have:

bool checkOut( const std::vector<ManagePatrons>& vecPatrons, ...


What does it mean to have a vector of ManagePatrons?
Was This Post Helpful? 0
  • +
  • -

#10 flameon87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-December 12

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 06:16 PM

I have a vector of ManagePatrons so that it pulls the information of the Patrons that has been stored in the database by the user, and pushing it into the function of checkout(). Meaning that with me having a vector of ManagePatrons it pulls the information required to checkOut a book, dvd, or cd.
Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 06:19 PM

Quote

I have a vector of ManagePatrons

Quote

it pulls the information of the Patrons

Patrons or ManagePatrons?
Was This Post Helpful? 0
  • +
  • -

#12 flameon87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-December 12

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 06:24 PM

from what ive been taught about vectors i have tried to orchestrate the vector so that it calls the ManagePatrons file of code and then goes down to the Patrons() function to get the information required to successfully execute the checkOut functions code.
Was This Post Helpful? 0
  • +
  • -

#13 flameon87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-December 12

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 06:50 PM

Was I taught incorrectly? If so, any pointers to help me in the right direction? I want to learn from this as this is by far the hardest program I have had to write so far.
Was This Post Helpful? 0
  • +
  • -

#14 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 08:42 PM

Let me put in perhaps the most straightforward way possible.

Did you mean vector<ManagePatron> or vector<Patron>?
Was This Post Helpful? 2
  • +
  • -

#15 flameon87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-December 12

Re: Creating a Library management system in C++ and database in SQL

Posted 03 December 2012 - 08:47 PM

I get what your saying now and it should be Patron since it holds all the information about the patron.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2