-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileSTU.cpp
More file actions
98 lines (89 loc) · 2.51 KB
/
FileSTU.cpp
File metadata and controls
98 lines (89 loc) · 2.51 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
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
class student {
public:
int rollno;
char name[25], Class[5], grade;
float marks;
void gradecal() //function to calculate grade
{
if (marks >= 75) { grade = 'A'; }
else if (marks >= 60 && marks < 75) { grade = 'B'; }
else if (marks >= 50 && marks < 40) { grade = 'C'; }
else if (marks > 0 && marks < 40) { grade = 'D'; }
}
void getdata() // function to input datas in the files
{
cout << "Enter Name of the Student:";
cin >> name;
cout << "Enter Student's Roll Number:";
cin >> rollno;
cout << "Enter Student's Class:";
cin >> Class;
cout << "Enter Student's marks:";
cin >> marks;
gradecal();
}
void putdata() //function to display the data in the file
{ cout << name << " with Roll no:" << rollno << " has " << marks << " %marks and grade of " << grade << endl; }
} S;
void input(void) //function called for entering datas of students inside the file
{
char ans = 'Y';
ofstream filin;
filin.open("Stu.txt", ios::out | ios::app);
while (ans == 'Y' || ans == 'y') {
S.getdata();
filin.write((char *) &S, sizeof(student));
cout << "\n Want to enter more records?(y/n):";
cin >> ans;
}
filin.seekp(0);
filin.close();
}
void search(void)//search by roll no
{
int rn;
ifstream fout("Stu.txt", ios::in);
fout.seekg(0);
cout << "Enter rollno to be searched for:";
cin >> rn;
while (!fout.eof()) {
fout.read((char *) &S, sizeof(student));
if (S.rollno == rn) {
S.putdata();
break;
}
else { cout << "Rollno doesn't exists" << endl; }
}
fout.close();
}
void display(void) {
ifstream fout("Stu.txt", ios::in);
fout.seekg(0);
while (!fout.eof()) {
fout.read((char *) &S, sizeof(S));
S.putdata();
}
fout.close();
}
int main() { // Interface
int choice;
cout << " ACT1.Enter Students details \n ACT2.Search Student Details \n ACT3.Print All Student details";
cout << "\n Enter your choice for Act:";
cin >> choice;
switch (choice) {
case 1:
input();
break; //function call to input datas of student using class
case 2:
search();//function call to search student details using roll no of class
break;
case 3:
display();
}
return 0;
}