Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 00af6fa

Browse files
committed
OOP2 Lab9 updated
1 parent 522ca58 commit 00af6fa

File tree

3 files changed

+102
-64
lines changed

3 files changed

+102
-64
lines changed
-13.3 KB
Binary file not shown.

‎OOP2_Lab9/README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Practical Lab Assignment - File Handling
2+
3+
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.

‎OOP2_Lab9/U1910049_Lab9.cpp‎ renamed to ‎OOP2_Lab9/main.cpp‎

Lines changed: 89 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// All three programs are written here
1+
// All three programs are written here
22
// A menu driven program which allows to use all programs at the same time
33
// Done be Rustam Zokirov (U1910049)
44
// Last change done in April 13, 2020
@@ -11,23 +11,25 @@
1111

1212
using namespace std;
1313

14-
int main();
14+
int main();
1515

16-
void F_First_Program() {
16+
void F_First_Program()
17+
{
1718

1819
// creating a text file one.txt
1920
ofstream out_one;
2021
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+
{
2224
out_one << i << endl; // writing to file first ten even numbers
2325
}
2426
out_one.close(); // closing the file
2527

26-
2728
// creating a text file one.txt
2829
ofstream out_two;
2930
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+
{
3133
out_two << i << endl; // writing to file first ten multiples of five
3234
}
3335
out_two.close(); // closing the file
@@ -38,13 +40,13 @@ void F_First_Program() {
3840
in_one.open("one.txt"); // opening files
3941
in_two.open("two.txt");
4042

41-
4243
int total = 0;
4344
int num1 = 0;
4445
int num2 = 0;
4546

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
4850
in_one >> num1;
4951
in_two >> num2;
5052
}
@@ -65,111 +67,131 @@ void F_First_Program() {
6567

6668
in_total >> total;
6769
cout << "TOTAL: " << total << "\n\n"; // displaying the total in console
68-
70+
6971
in_total.close(); // closing the file after executing
7072
}
7173

74+
void F_Second_Program()
75+
{
7276

73-
void F_Second_Program(){
74-
75-
for (int k = 0; k < 1000; k++) {
77+
for (int k = 0; k < 1000; k++)
78+
{
7679
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+
7987
ofstream out_contacts("contacts.txt", ios::app);
8088

8189
string name, phone;
8290

8391
switch (_getch())
8492
{
8593
// case 49 is for adding a new contact into a list
86-
case 49: {
94+
case 49:
95+
{
8796
system("cls");
8897
cout << "Adding a new contact. Input a contact info:\n\n";
8998

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;
92103

93104
// 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
96107

97108
cout << "Successfully added!\n\n";
98-
99-
system("pause");
100-
}
101-
break;
102109

110+
system("pause");
111+
}
112+
break;
103113

104-
case 50: {
114+
case 50:
115+
{
105116
system("cls");
106117
ifstream in_contacts("contacts.txt"); // getting data from the file
107-
while (in_contacts >> name >> phone) {
118+
while (in_contacts >> name >> phone)
119+
{
108120
// displaying the data
109121
cout << left << setw(12) << name << "\t" << phone << endl;
110122
}
111123
in_contacts.close(); // closing the file
112124
system("pause");
113125
}
114-
break;
126+
break;
115127

116-
117-
case48: {
128+
case48:
129+
{
118130
main(); // to back to main menu
119131
}
120-
break;
132+
break;
121133

122-
default: {
134+
default:
135+
{
123136
cout << "Your choice is not available in Menu.\nPlease try one more time\n";
124137
system("pause");
125138
}
126-
break;
139+
break;
127140
} // switch
128-
} // for loop
141+
} // for loop
129142
}
130143

144+
void F_Third_Program()
145+
{
131146

132-
void F_Third_Program(){
133-
134-
for (int k = 0; k < 1000; k++) {
147+
for (int k = 0; k < 1000; k++)
148+
{
135149
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";
137156

138157
ofstream out_numbers("numbers.txt", ios::app); // the list could be contiunied after the program execution
139158

140159
int numbers;
141-
160+
142161
switch (_getch())
143162
{
144-
case 49: {
163+
case 49:
164+
{
145165
system("cls");
146166
cout << "ENYER NUMBERS\n";
147167

148168
// 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;
151173
out_numbers << numbers << endl;
152174
}
153-
out_numbers.close(); //closing the file
175+
out_numbers.close(); //closing the file
154176

155-
156177
system("pause");
157178
}
158-
break;
159-
179+
break;
160180

161-
case 50: {
181+
case 50:
182+
{
162183
system("cls");
163184
ifstream in_numbers("numbers.txt");
164185

165186
cout << "SEARCHING A NUMBER\n";
166187

167188
int search_number;
168189
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: ";
170191
cin >> search_number;
171192

172-
while (in_numbers) {
193+
while (in_numbers)
194+
{
173195
in_numbers >> numbers;
174196
if (search_number == numbers)
175197
isAnswerHere = 1;
@@ -182,28 +204,35 @@ void F_Third_Program(){
182204

183205
system("pause");
184206
}
185-
break;
207+
break;
186208

187-
188-
case48: {
209+
case48:
210+
{
189211
main();
190212
}
191-
break;
213+
break;
192214

193-
default: {
215+
default:
216+
{
194217
cout << "Your choice is not available in Menu.\nPlease try one more time\n";
195218
system("pause");
196219
}
197-
break;
220+
break;
198221
} // switch
199-
} // for loop
222+
} // for loop
200223
}
201224

202-
203-
int main(){
204-
for (int k = 0; k < 1000; k++) {
225+
int main()
226+
{
227+
for (int k = 0; k < 1000; k++)
228+
{
205229
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";
207236

208237
switch (_getch())
209238
{
@@ -213,33 +242,29 @@ int main(){
213242
system("pause");
214243
break;
215244

216-
217245
case 50:
218246
system("cls");
219247
F_Second_Program();
220248
system("pause");
221249
break;
222250

223-
224251
case 51:
225252
system("cls");
226253
F_Third_Program();
227254
system("pause");
228255
break;
229256

230-
231257
case 48:
232258
return 0;
233259
break;
234260

235-
236261
default:
237262
cout << "Your choice is not available in Menu.\nPlease try one more time\n";
238263
system("pause");
239264
break;
240265

241266
} // switch
242-
} // for loop
267+
} // for loop
243268

244269
system("pause");
245270
return 0;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /