In Cpp, Use vectors to compute students score

#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>

using namespace std;

bool read_in(vector<char> *store, char *path){
  ifstream inf(path);
  if(inf){
    char c;
    while(inf >> c){
      store->push_back(c);
    }
    inf.close();
    return true;
  }
  cout << "Error: Unable to load/read file: " << path << endl;
  return false;
}

int main(){
  vector<char> answers;
  if(!read_in(&answers, (char*)"CorrectAnswers.txt")){
    cin.get();
    return 1;
  }
  vector<char> student;
  if(!read_in(&student, (char*)"StudentAnswers.txt")){
    cin.get();
    return 2;
  }
  if(answers.size() != student.size()){
    cout << "Error: No. of questions & answers mismatch!" << endl;
    cin.get();
    return 3;
  }

  int missed = 0, correct = 0;
  for(int i = answers.size()-1; 0 <= i; i --){
    if(answers[i] != student[i]){
      missed ++;
    } else {
      correct ++;
    }
  }

  cout << "The total number of questions missed: " << missed << endl;

  float percent = ((correct/(answers.size()*1.0f)) * 100.0f);
  cout << "The percentage of questions answered correctly: " << setprecision(2) << fixed << percent << endl;
  cin.get();
  return 0;
}
Download

Comments

Popular posts from this blog

Tecq Mate | Build APK with command line only | Build Android with cmd | No IDE | No Android Studio