-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyPhonebook.cpp
149 lines (120 loc) · 4.1 KB
/
MyPhonebook.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
void writeEntry(const string& filename, const string& name, const string& phone, int entryNumber) {
ofstream outFile(filename.c_str(), ios::app);
if (!outFile) {
cerr << "Error opening file for writing." << endl;
return;
}
outFile << entryNumber << ". " << name << ": " << phone << endl;
outFile.close();
}
void deleteEntry(const string& filename, int entryNumber) {
ifstream inFile(filename.c_str());
ofstream tempFile("temp.txt");
if (!inFile || !tempFile) {
cerr << "Error opening file for reading or writing." << endl;
return;
}
string line;
while (getline(inFile, line)) {
// Extract the number from the start of the line
istringstream lineStream(line);
int number;
char dot;
lineStream >> number >> dot;
if (number != entryNumber) {
tempFile << line << endl;
}
}
inFile.close();
tempFile.close();
remove(filename.c_str());
rename("temp.txt", filename.c_str());
cout << "Contact successfully deleted." << endl;
}
void displayEntries(const string& filename) {
ifstream inFile(filename.c_str());
if (!inFile) {
cerr << "Error opening file for reading." << endl;
return;
}
cout << "\n"; // Output vertical tab before the entries
cout << "\t";
string line;
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
}
int main() {
string filename = "data.txt";
string dataToWrite_Name;
string dataToWrite_Phone;
int entryNumber = 1; // To keep track of the entry number
cout << "Welcome to MyPhoneBook!" << endl;
while (true) {
cout << "\n"<<"\t"<<"1. Add entry" << endl;
cout << "\t2. Delete entry" << endl;
cout << "\t3. Display entries" << endl;
cout << "\t4. Clear all data" << endl;
cout << "\t5. Exit" << endl;
cout << "\tChoose an option: ";
int choice;
cin >> choice;
cin.ignore(); // Ignore newline character left in the buffer
switch (choice) {
case 1:
// Get the name and phone number
cout << "\tPlease enter Name and Phone number (Enter -1 to end entry)" << endl;
cout << "\tName: ";
getline(cin, dataToWrite_Name);
if (dataToWrite_Name == "-1") {
continue;
}
cout << "\tPhone: ";
getline(cin, dataToWrite_Phone);
if (dataToWrite_Phone == "-1") {
continue;
}
// Write entry to file
writeEntry(filename, dataToWrite_Name, dataToWrite_Phone, entryNumber);
entryNumber++; // Increment the entry number for the next entry
break;
case 2:
{
// Display entries and ask for the number to delete
displayEntries(filename);
cout << "\tEnter the number of the contact to delete: ";
int numberToDelete;
cin >> numberToDelete;
cin.ignore(); // Ignore newline character left in the buffer
deleteEntry(filename, numberToDelete);
}
break;
case 3:
displayEntries(filename);
break;
case 4:
{
ofstream outFile(filename.c_str(), ios::trunc);
if (!outFile) {
cerr << "\tError opening file for clearing." << endl;
return 1;
}
outFile.close();
cout << "\tAll data has been cleared." << endl;
}
break;
case 5:
return 0;
default:
cout << "\tInvalid option, please try again." << endl;
}
}
return 0;
}