include <iostream> #include "main.h" #include <string.h> #include <stdio.h> #include <time.h> #include <fstream> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { int outerChoice = 0; while(outerChoice!=9){ system("cls"); cout<<"\n Library Management Version 1.0 \n\n"; cout<<"\n1. Amin"; cout<<"\n2. User"; cout<<"\n9. Exit"; cout<<"\n\nEnter your choice : "; cin>>outerChoice; if( outerChoice == 1 ) { char adminUser[10]; char adminPassword[10]; system("cls"); cout<<"\n ---------- Admin Login -------"; cout<<"\n\n Enter username: "; cin>>adminUser; cout<<"\n Enter password:"; cin>>adminPassword; if( (strcmp(adminUser, ADMINUSER) == 0) && ( strcmp(adminPassword, ADMINPASSWORD) == 0)){ getchar(); getchar(); system("cls"); int innerChoice = 0; while(innerChoice != 9){ cout<<"\n\n Admin Login is Successful !! Press any key to continue.."; cout<<"\n1. Add Book"; cout<<"\n2. Update Book"; cout<<"\n3. Delete Book"; cout<<"\n4. List of All Books"; cout<<"\n9. Exit"; cout<<"\n\n Enter your choice : "; cin>>innerChoice; switch(innerChoice){ case 1: addBook(); break; case 2: updateBook(); break; case 3: deleteBook(); break; case 4: listOfAllBooks(); break; default: cout<<"\n\n Invalid Choice. Press any key to continue.."; getchar(); } system("cls"); } } else { cout<<"\n\n Error : Invalid Credentials. Press any key to conitnue"; getchar();getchar(); } } else if(outerChoice ==2) { char studentUser[10]; char studentPassword[10]; system("cls"); cout<<"\n ---------- Student Login -------"; cout<<"\n\n Enter username: "; cin>>studentUser; cout<<"\n Enter password:"; cin>>studentPassword; if( (strcmp(studentUser, USER) ==0) && (strcmp(studentPassword, USERPASSWORD) == 0)){ int innerChoice = 0; while(innerChoice != 9){ system("cls"); cout<<"\n1. Borrow a Book"; cout<<"\n2. Return a Book"; cout<<"\n3. List of All Books"; cout<<"\n9. Exit"; cout<<"\n\n Enter your choice : "; cin>>innerChoice; switch(innerChoice){ case 1: borrowBook();break; case 2: returnBook();break; case 3: listOfAllBooks(); break; case 9: break; default: cout<<"\n\n Error: Invalid Choice. Press any key to continue.."; getchar();getchar(); } } system("cls"); } else { cout<<"\n\n Error : Invalid Credentials. Press any key to conitnue"; getchar();getchar(); } } else if(outerChoice!=9){ cout<<"\n\n Invalid choice. Press any key to continue.."; getchar();getchar(); } else { cout<<"\n\n Thank you for using it !! Press any key to exit"; getchar();getchar(); } } return 0; } int addBook(){ int bookid; char bookname[20]; char bookauthor[20]; int year, month, day; time_t t = time(NULL); struct tm tm = *localtime(&t); year = tm.tm_year + 1900; month = tm.tm_mon +1; day = tm.tm_mday; system("cls"); cout<<"\n ---- Add a new Book ----"; cout<<"\n\n Enter 4 digit numberic id :"; cin>>bookid; cout<<"\n Enter book name : "; cin>>bookname; cout<<"\n Enter book author : "; cin>>bookauthor; char filename[20]; sprintf(filename,"%d%s",bookid,".dat"); ofstream file(filename); file<<bookname<<"\n"; file<<bookauthor<<"\n"; file<<year<<"\n"; file<<month<<"\n"; file<<day<<"\n"; file.close(); cout<<"\n File Saved Successfully !!"; getchar(); getchar(); } int deleteBook(){ } int updateBook(){ } int listOfAllBooks(){ } int borrowBook(){ } int returnBook(){ } int adminLogin(){ } int userLogin(){ } [09:17, 1/11/2021] Ankita Patil: /--------------- End of Main.cpp -----------/ [09:17, 1/11/2021] Ankita Patil: --------------- End of Main.cpp -----------*/ /* ---------------- main.h ----------------- */ #define ADMINUSER "admin" #define ADMINPASSWORD "admin123" #define USER "user" #define USERPASSWORD "user123" #define FINE 500 int addBook(); int deleteBook(); int updateBook(); int listOfAllBooks(); int borrowBook(); int returnBook(); int adminLogin(); int userLogin(); /* - -------------------- end of main.h -----------------*/
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}