this is the test.dat this is the answer .dat I need help please with source code
ID: 3812318 • Letter: T
Question
this is the test.dat
this is the answer .dat
I need help please with source code and output
Explanation / Answer
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
ifstream file("answer.dat");
string ans;
while (file && getline(file, ans)){
if (ans.length() == 0)continue;
}
for (auto const& c : ans)
std::cout << c << endl;
int totalStudents = 0;
cout << " Student-ID " << "Marks" << endl;
ifstream file1("test.dat"); // pass file name as argment
string linebuffer;
int options[20][5] = {{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};
while (file1 && getline(file1, linebuffer)){
if (linebuffer.length() == 0)continue;
string buf; // Have a buffer string
stringstream ss(linebuffer); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words
int marks = 0;
while (ss >> buf)
tokens.push_back(buf);
string sans = tokens.at(1);
for (int i=0;i<ans.length();i++){
if('A'==ans[i]){
options[i][0]++;
}
else if(ans[i]=='B'){
options[i][1]++;
}
else if(ans[i]=='C'){
options[i][2]++;
}
else if(ans[i]=='D'){
options[i][3]++;
}
else {
options[i][4]++;
}
if(ans[i]==sans[i]){
marks++;
// cout << "true"<<endl;
}
}
totalStudents++;
// tokens.push_back(marks+"");
cout << " "<<tokens.at(0) <<" "<<marks<<endl;
}
cout <<"Total number of students who took test:" << totalStudents <<endl;
cout << " question Number " << " A " << " B " << " C " << " D " << " E " <<endl;
// output each array element's value
for ( int i = 0; i < 20; i++ ){
int a = ans[i] - 65;
// cout << ans[i] << " "<< a;
cout <<" "<< i+1 << " ";
for ( int j = 0; j < 5; j++ ) {
if(j==a){
cout << options[i][j]<<"*"<<" ";
}
else{
cout << options[i][j]<<" ";
}
}
cout <<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.