Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <string>
- #include <ctype.h>
- int main() {
- unsigned int num_students {0};
- unsigned int class_sum {0};
- std::string answers {""};
- unsigned int line_number {0};
- std::cout << "Student Score" << std::endl;
- std::cout << "--------------------------" << std::endl;
- std::ifstream file;
- file.open("data/students.txt"); // pseudo path
- if (!file || !file.is_open()) {
- std::cerr << "Error: There was a problem opening the file." << std::endl;
- file.close();
- return -1;
- }
- std::string line;
- while(getline(file, line)) {
- if (std::all_of(begin(line), end(line), [](char c) { return std::isspace(c); })) continue;
- // Set the answers
- if (line_number == 0) {
- answers = line;
- }
- int student_grade {0};
- // If line is students answers
- if (line_number != 0 && line_number % 2 == 0) {
- for (int answer = 0; answer < answers.length(); answer++) {
- if (line[answer] == answers[answer]) {
- student_grade += 1;
- }
- }
- class_sum += student_grade;
- std::cout << student_grade << std::endl;
- } else if (line_number > 0) {
- // Line is students name
- std::cout << line << std::string(23 - line.length(), ' ');
- num_students += 1;
- }
- line_number += 1;
- student_grade = 0;
- }
- file.close();
- std::cout << "\n--------------------------" << std::endl;
- std::cout << "Average score " << " " << (class_sum / num_students) << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement