-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
137 lines (115 loc) · 3.62 KB
/
main.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <vector>
#include <string>
#include "Book.h"
#ifdef _WIN32
#include <windows.h> // For Windows-specific commands
#endif
using namespace std;
// Function to clear the screen
void clearScreen() {
#ifdef _WIN32
system("cls"); // For Windows
#else
system("clear"); // For Linux/macOS
#endif
}
// Function to add a book to the list
void addBook(vector<Book>& books) {
string title, author, status;
clearScreen(); // Clear screen before input
cout << "Enter book title: ";
getline(cin, title);
cout << "Enter author name: ";
getline(cin, author);
cout << "Enter status (read, currently reading, want to read): ";
getline(cin, status);
books.push_back(Book(title, author, status));
}
// Function to display the books
void displayBooks(const vector<Book>& books) {
clearScreen(); // Clear screen before displaying
if (books.empty()) {
cout << "No books in the list!" << endl;
return;
}
cout << "\nBooks in your tracker:" << endl;
for (size_t i = 0; i < books.size(); ++i) {
cout << "Book " << i + 1 << ":" << endl;
cout << "Title: " << books[i].title << endl;
cout << "Author: " << books[i].author << endl;
cout << "Status: " << books[i].status << endl;
cout << "-----------------------------" << endl;
}
// Wait for user input before clearing the screen
cout << "\nPress Enter to return to the menu...";
cin.ignore(); // Ignore the newline character from previous inputs
cin.get(); // Wait for user to press Enter
}
// Function to update a book's status
void updateBookStatus(vector<Book>& books) {
int index;
clearScreen(); // Clear screen before input
cout << "Enter the book number to update status: ";
cin >> index;
cin.ignore(); // Clear the input buffer
if (index < 1 || index > books.size()) {
cout << "Invalid book number!" << endl;
return;
}
string newStatus;
cout << "Enter new status (read, currently reading, want to read): ";
getline(cin, newStatus);
books[index - 1].status = newStatus;
cout << "Status updated!" << endl;
}
// Function to remove a book from the tracker
void removeBook(vector<Book>& books) {
int index;
clearScreen(); // Clear screen before input
cout << "Enter the book number to remove: ";
cin >> index;
cin.ignore(); // Clear the input buffer
if (index < 1 || index > books.size()) {
cout << "Invalid book number!" << endl;
return;
}
books.erase(books.begin() + index - 1);
cout << "Book removed!" << endl;
}
int main() {
vector<Book> books;
int choice;
do {
clearScreen(); // Clear screen before displaying the menu
cout << "--- Book Tracker Menu ---\n";
cout << "1. Add a new book\n";
cout << "2. Display all books\n";
cout << "3. Update book status\n";
cout << "4. Remove a book\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // Clear the input buffer
switch (choice) {
case 1:
addBook(books);
break;
case 2:
displayBooks(books);
break;
case 3:
updateBookStatus(books);
break;
case 4:
removeBook(books);
break;
case 5:
cout << "Exiting the program.\n";
break;
default:
cout << "Invalid choice! Please try again.\n";
}
} while (choice != 5);
return 0;
}