Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The file has values like this
BECDCBAADEBACBAEDDBEDCBAAECDCB
1234 BECDXACDXAXDXEDBXCABCDEXX

The first line are right answers and are to be compared to the answers in the second (only has one line because of testing reasons) and the integer is the id.

What I have tried:

#include <iostream>
#include <fstream>
using namespace std;
int main(){
    char right[30],r;
    char n=0,m=0;
    char answers[30],a;
    int id[30],i=0;


    ifstream infile;
    infile.open("exam.txt");

    ofstream outfile;
    outfile.open("report.txt");

    if (!infile.is_open())
        cout<<"Error opening file. Aborting....";

    if (!outfile.is_open())
        cout<<"Error opening file. Aborting....";



    infile>>r;
    while (!infile.eof()&& n<30 && i<30){
        right [n] = r;
        infile>>id[i];
        cout<<right [n];
        cout<<id[i];
        n++;
        m++;
    }
Posted
Updated 19-Mar-17 2:11am
Comments
Richard Andrew x64 18-Mar-17 18:59pm    
This is ALMOST a good question because it contains the problem and what has been tried. But it's missing the crucial ingredient of exactly what the question is. What is it supposed to do, and what is it doing wrong?
Member 13068214 18-Mar-17 21:09pm    
Oh here is the full thing

A multiple-choice examination consists of thirty questions. Each question has five choices, labeled A, B, C, D, and E. The first line of data contains the correct answers to the thirty questions in the first 30 character positions, for example:

BECDCBAADEBACBAEDDBEDCBAAECDCB

Each subsequent line contains the answers for a candidate. Data on a line consists of a candidate number (an integer), followed by one or more spaces, followed by the thirty answers given by the candidate in the next thirty consecutive character positions. An X is used if a candidate did not answer a partiular question. You may assume that a candidate number is followed by 30 valid responses A sample line is as follows:

1234 BECDXACDXAXDXEDBXCABCDEXX

There are an unknown number of candidates. A line containing a candidate number of 0 only indicates the end of the data.


Member 13068214 18-Mar-17 21:11pm    
The thing is I'm trying to read the correct answers to an array but I'm getting an error when i reach the next line with the integers and I'm also reading the blank spaces when I'm supposed to ignore it.
Richard MacCutchan 19-Mar-17 4:35am    
It would be much easier just to read each line into the array in one go using getline().
Member 13068214 19-Mar-17 6:13am    
I can't use getline(). I'm doing a beginners programming course

1 solution

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
 
Share this answer
 
Comments
Member 13068214 20-Mar-17 17:11pm    
I'm sorry but i never understood C

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900