-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvavg.cpp
More file actions
75 lines (62 loc) · 1.67 KB
/
csvavg.cpp
File metadata and controls
75 lines (62 loc) · 1.67 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
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class StudentProcessor {
string filename;
ifstream file;
public:
StudentProcessor(string fname) {
filename = fname;
file.open(filename);
if (!file.is_open()) {
cout << "Error opening file!" << endl;
}
}
// User-defined stoi function
int stringToInt(string s) {
int num = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] >= '0' && s[i] <= '9') {
num = num * 10 + (s[i] - '0');
} else {
// handle invalid character
cout << "Invalid number string: " << s << endl;
return 0;
}
}
return num;
}
void processStudents() {
string line;
while (getline(file, line)) {
stringstream ss(line);
string name, score1, score2, score3;
getline(ss, name, ',');
getline(ss, score1, ',');
getline(ss, score2, ',');
getline(ss, score3, ',');
int m1 = stringToInt(score1);
int m2 = stringToInt(score2);
int m3 = stringToInt(score3);
float avg = (m1 + m2 + m3) / 3.0;
if (avg >= 75) {
cout << "Name: " << name << ", Average: " << avg << endl;
}
}
}
~StudentProcessor() {
file.close();
}
};
int main() {
// char* fname = new char[100];
string fname;
cout << "Enter the CSV filename: ";
// cin >> fname;
fname="Marks.csv";
StudentProcessor sp(fname);
sp.processStudents();
// delete[] fname;
return 0;
}