-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent.cpp
More file actions
179 lines (165 loc) · 3.63 KB
/
student.cpp
File metadata and controls
179 lines (165 loc) · 3.63 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "student.h"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
void Student ::input()
{
cout << "Enter roll no: ";
cin >> rollno;
cin.ignore();
cout << "Enter name: ";
getline(cin, name);
cout << "Enter marks: ";
cin >> marks;
}
void Student::display() const
{
cout << "Roll No: " << rollno
<< ", Name: " << name
<< ", Marks: " << marks
<< ", Grade: " << calculategread() << endl;
}
char Student::calculategread() const
{
if (marks >= 90)
return 'A';
else if (marks >= 75)
return 'B';
else if (marks >= 60)
return 'C';
else if (marks >= 40)
return 'D';
else
return 'F';
}
string Student::toCSV() const
{
return to_string(rollno) + "," + name + "," + to_string(marks) + "," + calculategread();
}
void addstudent()
{
ofstream outfile("student.csv", ios::app);
Student s;
s.input();
outfile << s.toCSV() << endl;
cout << "Student added Successfully" << endl;
}
void displaystudent()
{
ifstream infile("student.csv");
string line;
while (getline(infile, line))
{
cout << line << endl;
}
}
void deletestudent()
{
ifstream infile("student.csv");
ofstream tempfile("temp.csv");
int rollno;
cout << "Enter the roll no to delete: ";
cin >> rollno;
string line;
bool deleted = false;
while (getline(infile, line))
{
stringstream ss(line);
string roll;
getline(ss, roll, ',');
if (stoi(roll) != rollno)
{
tempfile << line << endl;
}
else
{
deleted = true;
}
}
infile.close();
tempfile.close();
remove("student.csv");
rename("temp.csv", "student.csv");
if (deleted)
{
cout << "Student Deleted" << endl;
}
else
{
cout << "Student not found" << endl;
}
}
void searchstudent()
{
ifstream infile("student.csv");
int rollno;
cout << "Enter the roll no to search :";
cin >> rollno;
string line;
bool found = false;
while (getline(infile, line))
{
stringstream ss(line);
string rollStr, name, marksStr, grade;
getline(ss, rollStr, ',');
getline(ss, name, ',');
getline(ss, marksStr, ',');
getline(ss, grade, ',');
if (stoi(rollStr) == rollno)
{
cout << "Record Found: " << line << endl;
found = true;
break;
}
}
infile.close();
if (!found)
cout << "Student not found.\n";
}
void updateStudent()
{
ifstream infile("student.csv");
ofstream tempfile("temp.csv");
int roll;
cout << "Enter the rollno " << endl;
cin >> roll;
string line;
bool updated = false;
while (getline(infile, line))
{
stringstream ss(line);
string rollStr, name, marksStr;
getline(ss, rollStr, ',');
getline(ss, name, ',');
getline(ss, marksStr, ',');
if (stoi(rollStr) == roll)
{
Student s;
s.rollno = roll;
cout << "Enter new name: ";
cin.ignore();
getline(cin, s.name);
cout << "Enter new marks: ";
cin >> s.marks;
tempfile << s.toCSV() << endl;
updated = true;
}
else
{
tempfile << line << endl;
}
}
infile.close();
tempfile.close();
remove("student,csv");
rename("temp.csv", "student.csv");
if (updated)
{
cout << "Record Updated" << endl;
}
else
{
cout << "Student not Found" << endl;
}
}