Repository navigation
Expand file tree
/
Copy pathexercise5Modified.cpp
More file actions
132 lines (113 loc) · 3.21 KB
/
Copy pathexercise5Modified.cpp
File metadata and controls
132 lines (113 loc) · 3.21 KB
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
#include <iostream>
#include <fstream>
using namespace std;
void addRecord(string fullname, string email, string address){
fstream file;
file.open("Records.txt", ios::out | ios::app);
file << fullname << " / " << email << " / " << address << endl;
file.close();
}
class Record {
public:
string rFullname;
string rEmail;
string rAddress;
Record(string f, string e, string a){
rFullname = f;
rEmail = e;
rAddress = a;
}
void addPerson(){
addRecord(rFullname, rEmail, rAddress);
}
};
void showMenu(){
cout << "A. Add Records" << endl;
cout << "B. View Records" << endl;
cout << "C. Remove Records" << endl;
cout << "D. Exit" << endl;
}
void viewRecord(){
fstream file;
string line;
int counter = 0;
file.open("Records.txt", ios::in);
cout << "View record" << endl << endl;
while(getline(file, line)){
counter += 1;
cout << counter << " " << line << endl;
}
file.close();
}
void clearRecord(){
fstream file;
string line;
string strTemp = "";
string strLine;
int counter = 0;
int numLine = 0;
file.open("Records.txt", ios::in);
cout << "Clear record" << endl << endl;
cout << "Enter the line number to be removed: ";
getline(cin, strLine);
numLine = stoi(strLine);
while(getline(file, line)){
counter += 1;
if(counter != numLine){
strTemp = strTemp + line + "\n";
}
}
file.close();
file.open("Records.txt", ios::out);
file << strTemp;
file.close();
}
int main(){
char charChoice = 'x';
string strChoice;
while(charChoice != 'd'){
cout << "Record Keeping App" << endl << endl;
do{
showMenu();
cout << "Please enter your choice (a, b, c, or d): ";
getline (cin, strChoice);
if (strChoice.length() != 1 ){
cout << endl << "Please enter a single character only!" << endl;
}
} while(strChoice.length() != 1);
charChoice = tolower(strChoice[0]);
switch(charChoice) {
case 'a' :
{
string fName;
string fEmail;
string fAddress;
cout << "Name: ";
getline(cin, fName);
cout << "Email: ";
getline(cin, fEmail);
cout << "Address: ";
getline(cin, fAddress);
Record recs(fName, fEmail, fAddress);
recs.addPerson();
break;
}
case 'b' :
{
viewRecord();
cout << endl;
break;
}
case 'c' :
{
clearRecord();
cout << endl;
break;
}
case 'd' :
cout << "Exit" << endl;
break;
}
}
return 0;
}