The following program (in C not C++) reads single characters - I hope you will be able to convert to C++. It's rather long but the C++ version (with all the built in classes and methods) may be shorter.
#include <stdio.h>
#include <ctype.h>
#define CR 13
#define LF 10
#define NUMQUESTIONS 30
typedef struct
{
char standard[NUMQUESTIONS];
int candidatenum;
char ans[NUMQUESTIONS * 2 + 1];
char eval[NUMQUESTIONS * 7 + 15];
}
EXAM, *PEXAM;
int readstandard(PEXAM pex, FILE *fp);
int processpaper(PEXAM pex, FILE *fp);
char *infile = "exam.txt", *outfile = "report.txt";
void main()
{
FILE *fpin, *fpout;
static EXAM ex;
fpin = fopen(infile, "rb");
fpout = fopen(outfile, "w");
if(fpin == NULL || fpout == NULL)
{
goto eofunction;
}
readstandard(&ex, fpin);
while(processpaper(&ex, fpin))
{
fprintf(fpout, "Candidate: %d\nAnswers: %s\nEvaluation: %s\n\n", ex.candidatenum, ex.ans, ex.eval);
}
eofunction:
if(fpin == NULL)
{
printf("Could not open input file: %s\n", infile);
}
else
{
fclose(fpin);
}
if(fpout == NULL)
{
printf("Could not open output file: %s\n", outfile);
}
else
{
fclose(fpout);
}
}
int readstandard(PEXAM pex, FILE *fp)
{
char c;
int i = 0;
do
{
fread(&c, 1, 1, fp);
if(i < NUMQUESTIONS && isupper(c))
{
pex->standard[i++] = c;
}
}
while(c != LF);
return i;
}
int processpaper(PEXAM pex, FILE *fp)
{
char c = 0;
int i = 0, written = 0, tot = 0, score;
pex->candidatenum = 0;
do
{
if(fread(&c, 1, 1, fp) > 0)
{
if(isdigit(c))
{
pex->candidatenum = pex->candidatenum * 10 + (c - '0');
}
if(i < NUMQUESTIONS && isupper(c))
{
sprintf(pex->ans + i * 2, "%c ", c);
tot += score = c == 'X' ? 0 : (c == pex->standard[i] ? 2 : -1);
written += sprintf(pex -> eval + written, "%d. %d ", i + 1, score);
i++;
}
}
}
while(c != LF && c);
if(i)
{
sprintf(pex -> eval + written, "Total: %d", tot);
}
return i;
}
Sample input file:
Quote:
BECDCBAADEBACBAEDDBEDCBAAECDCB
1234 BEABCDXACDXAXDXEDBXCAXEBDXDEXX
117 ACBEXXXACDXAXDXEDBXCABCXBAEDDE
23456 BECDXACDXBEDCBAAEAXDXEDBXCABCX