Question
Can you please tell why c++ file show error when I add students.csv file. --students.csv file Abraham Simpson,324543,3.9 Agnes Skinner,470546,4.0 Akira Kurosawa,411928,2.1 Alice Glick,459608,3.3 Allison
Can you please tell why c++ file show error when I add students.csv file.
--students.csv file
Abraham Simpson,324543,3.9 Agnes Skinner,470546,4.0 Akira Kurosawa,411928,2.1 Alice Glick,459608,3.3 Allison Taylor,747954,2.3 Apu Nahasapeemapetilon,290816,4.0 Artie Ziff,245446,3.6 Baby Gerald,155387,3.9 Barney Gumble,262592,1.7 Bart Simpson,753102,1.8 Bernice Hibbert,242653,3.9 Brandine Spuckler,584416,1.7 Bumblebee Man,603577,3.1 Carl Carlson,241968,3.0 Chazz Busby,362030,3.7 Cletus Spuckler,653783,2.9 Comic Book Guy,234272,3.7 Dewey Largo,910346,2.5 Disco Stu,736389,4.0 Dolph Starbeam,829982,3.9 Drederick Tatum,937537,2.3 Edna Krabappel,877842,1.6 Elizabeth Hoover,389470,3.5 Gil Gunderson,383513,1.7 Gloria Jailbird,601956,3.2 Groundskeeper Willie,774965,2.8 Hans Moleman,217944,2.3 Helen Lovejoy,686009,1.8 Herman Hermann,164996,2.2 Homer Simpson,413084,2.1 Jacqueline Bouvier,522856,2.3 Janey Powell,919159,2.2 Jasper Beardly,417008,3.6 Jessica Lovejoy,667331,1.9 Jimbo Jones,718588,3.6 Johnny Tightlips,840329,3.3 Judge Roy Snyder,533451,1.8 Kearney Zzyzwicz,758048,2.8 Kent Brockman,568727,1.7 Kirk Van Houten,695606,1.6 Krusty The Clown,901832,4.0 Kumiko Albertson,339422,3.5 Lenny Leonard,562762,2.6 Lindsey Naegle,570423,4.0 Ling Bouvier,249669,4.0 Lionel Hutz,212516,2.8 Lisa Simpson,693664,2.3 Luann Van Houten,737447,2.7 Luigi Risotto,757705,2.5 Lunchlady Doris,895470,3.0 Maggie Simpson,712478,3.1 Manjula Nahasapeemapetilon,236261,2.3 Marge Simpson,695580,3.1 Martin Prince,575687,2.3 Maude Flanders,272754,2.3 Mayor Joe Quimby,425163,3.0 Milhouse Van Houten,619711,2.9 Miss Springfield,855862,3.5 Moe Szyslak,219634,3.2 Mona Simpson,419417,3.4 Ned Flanders,746703,2.5 Nelson Muntz,584760,2.4 Old Jewish Man,259301,2.3 Patty Bouvier,544545,3.1 Rabbi Hyman Krustofsky,374366,3.5 Rainier Wolfcastle,674248,2.7 Ralph Wiggum,414067,3.5 Rod Flanders,823458,2.5 Roger Meyers Jr.,313590,2.1 Ruth Powers,231018,3.7 Sarah Wiggum,920562,2.3 Sea Captain,544294,3.7 Selma Bouvier,217994,3.8 Seymour Skinner,434336,1.7 Shauna Chalmers,828516,2.1 Sideshow Mel,799960,2.9 Snake Jailbird,950955,1.8 Squeaky-Voiced Teen,194002,1.8 Surly Duff,146127,3.4 The Rich Texan,300288,2.8 Todd Flanders,257297,2.3 Troy McClure,394769,1.9 Uter Zorker,712465,2.2 Waylon Smithers,387433,3.0 Wendell Borton,677806,1.7 Wise Guy,134681,1.8
------------
--C++ code
#include
#include
#include
using namespace std;
struct Student {
int sid;
string name;
double gpa;
};
bool loadStudents(Student students[], int &count);
void queryStudents(Student students[], int count);
bool parseQuery(string query, char &op, double &value);
int main() {
Student students[86];
int count = 0;
if (loadStudents(students, count)) {
queryStudents(students, count);
} else {
cout << "Failed to load students." << endl;
}
cout << "Goodbye!" << endl;
return 0;
}
bool loadStudents(Student students[], int &count) {
ifstream file("students.csv");
if (!file.is_open()) {
return false;
}
string line;
while (getline(file, line)) {
stringstream ss(line);
string field;
getline(ss, field, ',');
students[count].sid = stoi(field);
getline(ss, students[count].name, ',');
getline(ss, field, ',');
students[count].gpa = stod(field);
count++;
}
return true;
}
void queryStudents(Student students[], int count) {
while (true) {
cout << "Enter GPA query... ";
string query;
getline(cin, query);
if (query.length() == 1 && query[0] == '!') {
cout << "Exit the program? (Y)es/(N)o: ";
string exit;
getline(cin, exit);
if (exit == "Y" || exit == "y") {
break;
} else {
continue;
}
}
char op;
double value;
if (!parseQuery(query, op, value)) {
cout << "Syntax error: ? [Op][value]
cout << "Op: [>,<,~,!]" << endl;
cout << "value: GPA value" << endl;
continue;
}
int matchCount = 0;
for (int i = 0; i < count; i++) {
switch (op) {
case '>':
if (students[i].gpa > value) {
matchCount++;
cout << "[" << matchCount << "] " << students[i].sid << ": " << students[i].gpa << " (" << students[i].name << ")" << endl;
}
break;
case '<':
if (students[i].gpa < value) {
matchCount++;
cout << "[" << matchCount << "] " << students[i].sid << ": " << students[i].gpa << " (" << students[i].name << ")" << endl;
}
break;
case '~':
if (students[i].gpa >= value && students[i].gpa < value + 0.1) {
matchCount++;
cout << "[" << matchCount << "] " << students[i].sid << ": " << students[i].gpa << " (" << students[i].name << ")" << endl;
}
break;
}
}
}
}
bool parseQuery(string query, char &op, double &value) {
if (query.length() < 3) {
return false;
}
return true;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started