-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-reading-task-2.cpp
68 lines (61 loc) · 1.56 KB
/
file-reading-task-2.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
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
struct Student {
int rollNo;
string name;
string city;
string phone;
};
void addStudent() {
ofstream file;
file.open("student.txt", ios::app);
Student student;
cout << "Enter roll number: ";
cin >> student.rollNo;
cout << "Enter name: ";
getline(cin, student.name);
cout << "Enter city: ";
getline(cin, student.city);
cout << "Enter phone: ";
getline(cin, student.phone);
file << student.rollNo << " " << student.name << " " << student.city << " " << student.phone << endl;
file.close();
}
void viewStudents() {
ifstream file;
file.open("student.txt");
Student student;
while (file >> student.rollNo >> student.name >> student.city >> student.phone) {
cout << "Roll No: " << student.rollNo << endl;
cout << "Name: " << student.name << endl;
cout << "City: " << student.city << endl;
cout << "Phone: " << student.phone << endl;
cout << endl;
}
file.close();
}
int main() {
int choice;
while (true) {
cout << "1) Enter new student" << endl;
cout << "2) View all students" << endl;
cout << "3) Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
viewStudents();
break;
case 3:
exit(0);
default:
cout << "Invalid choice" << endl;
}
}
return 0;
}