You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Create two file “one.txt” and “two.txt” which contains first 10 even numbers and first ten multiples of 5 respectively; Read the two files, find the sum of all the number of these two files and store it in the variable TOTAL. Write this value in third file named “total.txt”.
4
+
5
+
2. A file contains a list of telephone numbers in the following form:
6
+
```
7
+
John 23456
8
+
Ahmed 9876
9
+
... ...
10
+
```
11
+
The names contain only one word and the names and telephone numbers are separated by white spaces. Write a program to read the file and output the list in the two columns.
12
+
13
+
3. Enter 20 numbers in a file named “Numbers.txt”. Ask user to enter any number and search if it exists in the file or not.
Copy file name to clipboardExpand all lines: OOP2_Lab9/main.cpp
+89-64Lines changed: 89 additions & 64 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
// All three programs are written here
1
+
// All three programs are written here
2
2
// A menu driven program which allows to use all programs at the same time
3
3
// Done be Rustam Zokirov (U1910049)
4
4
// Last change done in April 13, 2020
@@ -11,23 +11,25 @@
11
11
12
12
usingnamespacestd;
13
13
14
-
intmain();
14
+
intmain();
15
15
16
-
voidF_First_Program() {
16
+
voidF_First_Program()
17
+
{
17
18
18
19
// creating a text file one.txt
19
20
ofstream out_one;
20
21
out_one.open("one.txt");
21
-
for (int i = 2; i <= 2 * 10; i = i + 2) {
22
+
for (int i = 2; i <= 2 * 10; i = i + 2)
23
+
{
22
24
out_one << i << endl; // writing to file first ten even numbers
23
25
}
24
26
out_one.close(); // closing the file
25
27
26
-
27
28
// creating a text file one.txt
28
29
ofstream out_two;
29
30
out_two.open("two.txt");
30
-
for (int i = 5; i <= 5 * 10; i = i + 5) {
31
+
for (int i = 5; i <= 5 * 10; i = i + 5)
32
+
{
31
33
out_two << i << endl; // writing to file first ten multiples of five
32
34
}
33
35
out_two.close(); // closing the file
@@ -38,13 +40,13 @@ void F_First_Program() {
38
40
in_one.open("one.txt"); // opening files
39
41
in_two.open("two.txt");
40
42
41
-
42
43
int total = 0;
43
44
int num1 = 0;
44
45
int num2 = 0;
45
46
46
-
while (in_one && in_two) {
47
-
total += num1 + num2; // calculating the total
47
+
while (in_one && in_two)
48
+
{
49
+
total += num1 + num2; // calculating the total
48
50
in_one >> num1;
49
51
in_two >> num2;
50
52
}
@@ -65,111 +67,131 @@ void F_First_Program() {
65
67
66
68
in_total >> total;
67
69
cout << "TOTAL: " << total << "\n\n"; // displaying the total in console
68
-
70
+
69
71
in_total.close(); // closing the file after executing
70
72
}
71
73
74
+
voidF_Second_Program()
75
+
{
72
76
73
-
voidF_Second_Program(){
74
-
75
-
for (int k = 0; k < 1000; k++) {
77
+
for (int k = 0; k < 1000; k++)
78
+
{
76
79
system("cls");
77
-
cout << "C O N T A C T S\n" << "------------------\n" << "1. Add a contact\n" << "2. Contacts\n" << "0. Back\n" << "Your choice: \n";
78
-
80
+
cout << "C O N T A C T S\n"
81
+
<< "------------------\n"
82
+
<< "1. Add a contact\n"
83
+
<< "2. Contacts\n"
84
+
<< "0. Back\n"
85
+
<< "Your choice: \n";
86
+
79
87
ofstream out_contacts("contacts.txt", ios::app);
80
88
81
89
string name, phone;
82
90
83
91
switch (_getch())
84
92
{
85
93
// case 49 is for adding a new contact into a list
86
-
case49: {
94
+
case49:
95
+
{
87
96
system("cls");
88
97
cout << "Adding a new contact. Input a contact info:\n\n";
89
98
90
-
cout << "Enter the name: "; cin >> name;
91
-
cout << "Enter the phone number: "; cin >> phone;
99
+
cout << "Enter the name: ";
100
+
cin >> name;
101
+
cout << "Enter the phone number: ";
102
+
cin >> phone;
92
103
93
104
// storing the data in file
94
-
out_contacts << left << setw(12) << name << "\t" << phone << "\n";
95
-
out_contacts.close(); //closing the file
105
+
out_contacts << left << setw(12) << name << "\t" << phone << "\n";
106
+
out_contacts.close(); //closing the file
96
107
97
108
cout << "Successfully added!\n\n";
98
-
99
-
system("pause");
100
-
}
101
-
break;
102
109
110
+
system("pause");
111
+
}
112
+
break;
103
113
104
-
case50: {
114
+
case50:
115
+
{
105
116
system("cls");
106
117
ifstream in_contacts("contacts.txt"); // getting data from the file
107
-
while (in_contacts >> name >> phone) {
118
+
while (in_contacts >> name >> phone)
119
+
{
108
120
// displaying the data
109
121
cout << left << setw(12) << name << "\t" << phone << endl;
110
122
}
111
123
in_contacts.close(); // closing the file
112
124
system("pause");
113
125
}
114
-
break;
126
+
break;
115
127
116
-
117
-
case48: {
128
+
case48:
129
+
{
118
130
main(); // to back to main menu
119
131
}
120
-
break;
132
+
break;
121
133
122
-
default: {
134
+
default:
135
+
{
123
136
cout << "Your choice is not available in Menu.\nPlease try one more time\n";
124
137
system("pause");
125
138
}
126
-
break;
139
+
break;
127
140
} // switch
128
-
} // for loop
141
+
}// for loop
129
142
}
130
143
144
+
voidF_Third_Program()
145
+
{
131
146
132
-
voidF_Third_Program(){
133
-
134
-
for (int k = 0; k < 1000; k++) {
147
+
for (int k = 0; k < 1000; k++)
148
+
{
135
149
system("cls");
136
-
cout << "S E A R C H I N G F O R N U M B E R \n" << "------------------------------------\n" << "1. Add numbers\n" << "2. Search for number\n" << "0. Back\n" << "Your choice: \n";
150
+
cout << "S E A R C H I N G F O R N U M B E R \n"
151
+
<< "------------------------------------\n"
152
+
<< "1. Add numbers\n"
153
+
<< "2. Search for number\n"
154
+
<< "0. Back\n"
155
+
<< "Your choice: \n";
137
156
138
157
ofstream out_numbers("numbers.txt", ios::app); // the list could be contiunied after the program execution
139
158
140
159
int numbers;
141
-
160
+
142
161
switch (_getch())
143
162
{
144
-
case49: {
163
+
case49:
164
+
{
145
165
system("cls");
146
166
cout << "ENYER NUMBERS\n";
147
167
148
168
// inputing numbers
149
-
for (int i = 1; i <= 20; i++) {
150
-
cout << "[ " << i << " ] -> "; cin >> numbers;
169
+
for (int i = 1; i <= 20; i++)
170
+
{
171
+
cout << "[ " << i << " ] -> ";
172
+
cin >> numbers;
151
173
out_numbers << numbers << endl;
152
174
}
153
-
out_numbers.close(); //closing the file
175
+
out_numbers.close(); //closing the file
154
176
155
-
156
177
system("pause");
157
178
}
158
-
break;
159
-
179
+
break;
160
180
161
-
case50: {
181
+
case50:
182
+
{
162
183
system("cls");
163
184
ifstream in_numbers("numbers.txt");
164
185
165
186
cout << "SEARCHING A NUMBER\n";
166
187
167
188
int search_number;
168
189
bool isAnswerHere = 0; // for finding the searching number from available list
169
-
cout << "Enter the number to search: ";
190
+
cout << "Enter the number to search: ";
170
191
cin >> search_number;
171
192
172
-
while (in_numbers) {
193
+
while (in_numbers)
194
+
{
173
195
in_numbers >> numbers;
174
196
if (search_number == numbers)
175
197
isAnswerHere = 1;
@@ -182,28 +204,35 @@ void F_Third_Program(){
182
204
183
205
system("pause");
184
206
}
185
-
break;
207
+
break;
186
208
187
-
188
-
case48: {
209
+
case48:
210
+
{
189
211
main();
190
212
}
191
-
break;
213
+
break;
192
214
193
-
default: {
215
+
default:
216
+
{
194
217
cout << "Your choice is not available in Menu.\nPlease try one more time\n";
195
218
system("pause");
196
219
}
197
-
break;
220
+
break;
198
221
} // switch
199
-
} // for loop
222
+
}// for loop
200
223
}
201
224
202
-
203
-
intmain(){
204
-
for (int k = 0; k < 1000; k++) {
225
+
intmain()
226
+
{
227
+
for (int k = 0; k < 1000; k++)
228
+
{
205
229
system("cls");
206
-
cout << "M A I N M E N U\n"<< "-------------------\n" << "1. First Program\n" << "2. Second Program\n" << "3. Third Program\n" << "Your choice: \n";
230
+
cout << "M A I N M E N U\n"
231
+
<< "-------------------\n"
232
+
<< "1. First Program\n"
233
+
<< "2. Second Program\n"
234
+
<< "3. Third Program\n"
235
+
<< "Your choice: \n";
207
236
208
237
switch (_getch())
209
238
{
@@ -213,33 +242,29 @@ int main(){
213
242
system("pause");
214
243
break;
215
244
216
-
217
245
case50:
218
246
system("cls");
219
247
F_Second_Program();
220
248
system("pause");
221
249
break;
222
250
223
-
224
251
case51:
225
252
system("cls");
226
253
F_Third_Program();
227
254
system("pause");
228
255
break;
229
256
230
-
231
257
case48:
232
258
return0;
233
259
break;
234
260
235
-
236
261
default:
237
262
cout << "Your choice is not available in Menu.\nPlease try one more time\n";
0 commit comments