Library management system
Jun 22, 2016 at 10:46pm UTC
I made a library management system that has a few problems. First of all, when you add a new book to the system it works but it automatically brings you to the checkout case after. Secondly, when you try and checkout an existing book, it says that it doesn't exist. Please tell me what I am doing wrong. I am using visual studio if that makes a difference. Thanks in advance!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool checkedout;
char user;
int x = 0;
string book;
fstream file;
string filename;
string extension = ".txt" ;
int checkout(string bookname)
{
file.open(filename.c_str(), ios_base::in | ios_base::out);
if (file.is_open())
{
file.open(filename.c_str(), ios_base::in);
file >> checkedout;
file.close();
if (checkedout == false )
{
checkedout = true ;
file.open(filename.c_str(), ios_base::in);
file << checkedout;
file.close();
cout << "Book checked out!" ;
}
else
{
cout << "Book already checked out!" ;
}
}
else
{
file.close();
cout << "Book not in system\n" ;
}
return x;
}
int add(string bookname)
{
filename = bookname + extension;
file.open(filename.c_str(), ios_base::in | ios_base::out);
if (file.is_open())
{
cout << "Book already in system." ;
file.close();
}
else
{
file.open(filename.c_str(), ios_base::out);
checkedout = false ;
file << checkedout;
file.close();
cout << "Book added!\n" ;
user = 0;
}
return x;
}
int main(int test)
{
cout << "Welcome to the libaray managment system!\n" ;
string password;
cout << "Enter password: " ;
getline(cin, password);
if (password == "library" ) {
while (true )
{
cout << "To add a book to the system, enter 'A'.\nTo check out a book, enter 'C'.\n" ;
cin >> user;
cin.ignore();
switch (user)
{
case 'A' :
case 'a' :
{
cout << "\nEnter name of book to add: " ;
getline(cin, book);
add(book);
}
case 'C' :
case 'c' :
{
cout << "\nEnter name of book to checkout: " ;
getline(cin, book);
checkout(book);
}
}
}
}
}
Jun 23, 2016 at 8:07am UTC
First of all, when you add a new book to the system it works but it automatically brings you to the checkout case after.
You need to add a
break ;
to the switch case. See:
http://www.cplusplus.com/doc/tutorial/control/
"Another selection statement: switch."
Secondly, when you try and checkout an existing book, it says that it doesn't exist. Please tell me what I am doing wrong.
In checkout(...) add line 44 before line 16.
Topic archived. No new replies allowed.