1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
using namespace std;
void createfile();
int main()
{
createfile();
const int AnsNumb = 20;
string line;
string CorrectAnswer[AnsNumb];//Array for Correct Answers
string StudentAnswer[AnsNumb];//Array for random Studen Answers
int count = 0, total = 0;
ifstream inputfile;
inputfile.open("testanswers.txt");
inputfile.seekg(2L, ios::beg);
for( int i = 0; i < AnsNumb; ++i) {
if(i > 9){
getline(inputfile, line, '\n');
CorrectAnswer[i] = line;
inputfile.seekg(3L, ios::cur);
}
else {
getline(inputfile, line, '\n');
CorrectAnswer[i] = line;
inputfile.seekg(2L, ios::cur);
}
}
inputfile.close();
inputfile.open("studentanswers.txt");
inputfile.seekg(2L, ios::beg);
for( int i = 0; i < AnsNumb; ++i) {
if(i > 9){
getline(inputfile, line, '\n');
StudentAnswer[i] = line;
inputfile.seekg(3L, ios::cur);
}
else {
getline(inputfile, line, '\n');
StudentAnswer[i] = line;
inputfile.seekg(2L, ios::cur);
}
}
inputfile.close();
for (int index = 0; index < AnsNumb; index++)
{
if (StudentAnswer[index] != CorrectAnswer[index])
{
total++;
}
}
cout << "Student missed " << total << " questions.\n";
cin.ignore();
return 0;
}
void createfile() {
const int numquestions = 20;
char studanswer[numquestions];
srand((int)time(0));
int numb[numquestions] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
ofstream outputfile;
//Stores the Array
for (int index = 0; index < 20; index++)
{
studanswer[index] = 65 + rand()% 4;
}
//Opens the file
outputfile.open("studentanswers.txt");
//Writes to the file
for (int index = 0; index < numquestions; index++)
{
outputfile << numb[index] << " " << studanswer[index] << endl;
}
//Closes file
outputfile.close();
cout << "The numbers were saved to a file\n";
cin.ignore();
return;
}
|