C++ project

profilejigmey_grg21
Source.cpp

#include<iostream> #include<vector> #include<string> #include<fstream> using namespace std; void gradeStd() { // Read from StudentAnswers.txt // and AnswersKey.txt ifstream stAns("StudentsAnswers.txt"); ifstream answer("AnswersKey.txt"); // These strings will store data // from both the text files vector<string> studentAnswer; vector<string> ansKey; string line; // Reading line by line from both the files and storing in vectors while (getline(stAns, line)) { studentAnswer.push_back(line); } while (getline(answer, line)) { ansKey.push_back(line); } // Calculating no. of students from number of commas int numStd = count(studentAnswer[0].begin(), studentAnswer[0].end(), ',') + 1; // This vector will store grades of each student vector<int> grade(numStd, 0); // Going through every question to check student's answer for (int i = 0; i < ansKey.size(); i++) { // answer to the current // question stored in 'ans' char ans = ansKey[i][0]; // Holds answers for every // student for current question string strAns = studentAnswer[i]; int stdNo = 0; // Stores the current student // Checking answers of every student one by one for (int j = 0; j < strAns.size(); j++) { if (strAns[j] == ',') { //If we hit comma"," then move to next std (col) stdNo++; continue; } if (ans == strAns[j]) { //if std's answer matches the original answer; Increase grade by 1 grade[stdNo] += 1; } } } for (int i = 0; i < grade.size(); i++) { cout << grade[i] << endl; } } int main() { gradeStd(); return 0; }