-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook record.cpp
151 lines (136 loc) · 4.05 KB
/
book record.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <fstream>
#include <iomanip> // to use setw function;
using namespace std;
class Record{
string Book_id, Book_price, Book_name , Book_author_name;
public:
void menu();
void insert();
void display();
void modify();
void search();
void deleted();
};
void Record::menu(){
menu:
int choice;
char x;
cout << "|---------------------------|" << endl;
cout << "| BOOKS RECORD SYSTEM |" << endl;
cout << "|---------------------------|" << endl;
cout << " 1. Enter New Record" << endl;
cout << " 2. Display all Records" << endl;
cout << " 3. Modify Record" << endl;
cout << " 4. Search Record" << endl;
cout << " 5. Exit\n"<< endl;
cout<<"Select an option : ";
cin >> choice;
switch (choice){
case 1:
do{
insert();
cout <<endl<<"Do you want to add another Record (Y/N) : ";
cin >> x;
} while (x == 'y' || x == 'Y');
break;
case 2:
display();
break;
case 3:
modify();
break;
case 4:
search();
break;
case 5:
cout << "\n\t Program Is Exit";
exit(0);
default:
cout << "\n\n Invalid Choice... Try Again...\n"<<endl;
}
goto menu;
}
void Record::insert(){
fstream file;
cout<<"\nEnter Book id : ";
cin >> Book_id;
cin.ignore();
cout<<"Enter the Book name : ";
getline(cin, Book_name);
cout<<"Enter the Author name : ";
getline(cin, Book_author_name);
cout<<"Enter the Price : ";
cin >> Book_price;
file.open("record.txt", ios::app | ios::out);
//setw() method to set the field width;
file << " " << Book_id << setw(30) << Book_name << setw(30) << Book_author_name << setw(10) << Book_price << endl;
file.close();
}
void Record::display(){
fstream file;
file.open("record.txt", ios::in);
if (!file){
cout << "\nNo Data is Present... "<<endl<<endl;
}
else{
string content;
cout<<endl<<"------------------Book Records--------------------"<<endl;
while (!file.eof()){
getline(file,content);
cout<< content <<endl;
}
}
file.close();
}
void Record::modify(){
string id, lineContent;
fstream file, temp;
cout << "\nEnter the book Id which you want to Modify: ";
cin >> id;
file.open("record.txt", ios::in);
temp.open("tmp.txt", ios::app | ios::out);
file >> Book_id;
while (getline(file,lineContent)){
if (id != Book_id)
temp << " " << Book_id << lineContent << endl;
else{
cout<<"\nEnter Book id : ";
cin >> Book_id;
cin.ignore();
cout<<"Enter the Book name : ";
getline(cin, Book_name);
cout<<"Enter the Author name : ";
getline(cin, Book_author_name);
cout<<"Enter the Price : ";
cin >> Book_price;
temp << " " << Book_id << setw(30) << Book_name << setw(30) << Book_author_name << setw(10) << Book_price << endl;
}
file >> Book_id;
}
file.close();
temp.close();
remove("record.txt");
rename("tmp.txt", "record.txt");
}
void Record::search(){
string id, line;
fstream file;
file.open("record.txt", ios::in);
cout<<"Enter the Id number of the book : ";
cin >> id;
file >> Book_id ;
while(getline(file, line)){
if (id == Book_id)
cout << " " << Book_id << line << endl;
else
file >> Book_id;
}
file.close();
}
int main()
{
Record r;
r.menu();
return 0;
}