Skip to content

Instantly share code, notes, and snippets.

@barbarbar338
Created April 12, 2025 10:03
Show Gist options
  • Save barbarbar338/93e2c86cb307a650225463a3310bf1ff to your computer and use it in GitHub Desktop.
Save barbarbar338/93e2c86cb307a650225463a3310bf1ff to your computer and use it in GitHub Desktop.
Checking how many questions students answered from a CSV table - Prepared for AGU EEE Coma Capsule Integrated Project#2
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <map>
#include <algorithm>
using namespace std;
// Using pointers to pass the vector by reference
void readCSV(vector<string> *vect) {
fstream file; // Create a file stream
file.open("file_list.csv", ios::in); // Open the file in read mode
string line;
while (getline(file, line)) {
if (line != "File Name")
vect->push_back(line);
}
// Gracefully close the file
file.close();
}
struct Group {
string code;
vector<string> questions;
};
struct Student {
string sid;
vector<Group> groups;
};
int main() {
// A vector to hold the answered questions
vector<string> vect;
readCSV(&vect);
// create a map of students
map<string, Student> students;
for (int i = 0; i < vect.size(); i++) {
string line = vect[i];
istringstream ss(line);
vector<string> split;
string token;
while (getline(ss, token, '_')) {
split.push_back(token);
}
string group, question, sid;
for (int j = 0; j < split.size(); j++) {
if (j == 0) {
group = split[j];
// make string all lowercase
for (int k = 0; k < group.size(); k++) {
group[k] = tolower(group[k]);
}
// trim the group
while (group[0] == ' ') {
group = group.substr(1);
}
while (group[group.size() - 1] == ' ') {
group = group.substr(0, group.size() - 1);
}
// get the latest element
group = group[group.size() - 1];
} else if (j == 1) {
question = split[j];
// make string all lowercase
for (int k = 0; k < question.size(); k++) {
question[k] = tolower(question[k]);
}
// trim the question
while (question[0] == ' ') {
question = question.substr(1);
}
while (question[question.size() - 1] == ' ') {
question = question.substr(0, question.size() - 1);
}
// get the latest element
question = question[question.size() - 1];
} else if (j == 2) {
sid = split[j];
// remove all the extension from the sid
int pos = sid.find('.');
if (pos != string::npos) {
sid = sid.substr(0, pos);
}
// remove all the items inside the parenthesis
pos = sid.find('(');
if (pos != string::npos) {
sid = sid.substr(0, pos);
}
// trim the sid
while (sid[0] == ' ') {
sid = sid.substr(1);
}
while (sid[sid.size() - 1] == ' ') {
sid = sid.substr(0, sid.size() - 1);
}
}
}
// check if the student exists
if (students.find(sid) == students.end()) {
// create a new student
Student student;
student.sid = sid;
// create a new group
Group studen_group;
studen_group.code = group;
studen_group.questions.push_back(question);
// add the group to the student
student.groups.push_back(studen_group);
// add the student to the map
students.insert(pair<string, Student>(sid, student));
} else {
// get the student
Student student = students[sid];
// check if the group exists
bool groupExists = false;
for (int j = 0; j < student.groups.size(); j++) {
Group student_group = student.groups[j];
if (student_group.code == group) {
groupExists = true;
// check if the question exists
bool questionExists = false;
for (int k = 0; k < student_group.questions.size(); k++) {
string q = student_group.questions[k];
if (q == question) {
questionExists = true;
break;
}
}
// add the question to the group
if (!questionExists) {
student_group.questions.push_back(question);
}
// update the group
student.groups[j] = student_group;
break;
}
}
// add the group to the student
if (!groupExists) {
Group student_group;
student_group.code = group;
student_group.questions.push_back(question);
student.groups.push_back(student_group);
}
// update the student
students[sid] = student;
}
}
// create csv file structure
string csv = "Student list,GroupName,QuestionsAnswered\n";
// print the map
for (auto it = students.begin(); it != students.end(); it++) {
string sid = it->first;
Student student = it->second;
cout << "SID: " << sid << endl;
for (auto it = student.groups.begin(); it != student.groups.end(); it++) {
Group group = *it;
cout << "Group: " << group.code << endl;
vector<string> questionVect = group.questions;
sort(questionVect.begin(), questionVect.end());
// joining with loop
string result;
for (const auto &str : questionVect) {
result += str + ", ";
}
// remove the last comma
if (!result.empty() && result.back() == ' ') {
result.pop_back();
}
if (!result.empty() && result.back() == ',') {
result.pop_back();
}
cout << "Questions Answered: " << result << endl;
cout << endl << "-------------------" << endl;
csv += sid + "," + group.code + ",\"" + result + "\"\n";
}
}
// write the csv to a file
ofstream file;
file.open("output.csv", ios::out);
file << csv;
file.close();
cout << "CSV file created successfully" << endl;
cout << "Please check the output.csv file" << endl;
cout << "-------------------" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment