Static_es2
Static_es2

Reputation: 1

Reading a text file and comparing each line

I have an assignment that has a txt file. I have to scan the first line and compare it to other line

(
BACCDEABCEEDCDABBAED
6734 BXCCDAABCEEDCDACBAED
7843 BADCXAABCEXXCDABBAED
2223 BCBAEACCDAEDCDABBAEA
2324 BACXDEABCEEDCDAABAED
3474 BACCDEABCEEDCDABBAED
3434 XADCDAABCEEDCDABBAED
6374 XXXXXCXXXXXXXXXXXAED
3332 BADCDEABCEEDCDADBCEX
3454 BACCXEABCEEDCXABBAED
0
)

How would I seperate the first for digits and compare the the [20](Correct_ answers) other letters with the first line of 20?

This is what I got so far.........

#include <stdio.h>
#include <stdlib.h>

int main(){
    char answer[20];

    char studentAnswer[20];

    int studentId[4];

    int x;

    int correct=0,wrong=0,notAttempted=0;

    FILE * in = fopen("/Users/MarkB/Desktop/ASSIGNMENT3/exam.txt", "r");
    FILE * out = fopen("/Users/MarkB/Desktop/ASSIGNMENT3/examRsults.txt", "w");

    fscanf(in,"%d",studentId);
    fscanf(in,"%s",answer);
    fscanf(in,"%s",studentAnswer);
    while (studentId !=0){
        for (x=0; x<20;x=x+1){
            if(answer[x]==studentAnswer[x]){
                correct=correct + 1;
            }
            if(answer[x]!=studentAnswer[x] && studentAnswer[x]!='X'){
                wrong=wrong + 1;
            }
            if(studentAnswer[x]=='X'){
                notAttempted=notAttempted + 1;
            }
        }
    }

    fprintf(out,"--------Student Exam results------\n");

    fprintf (out,"%d",studentId[0]);



    fprintf (out,"Questions Correct: %d \n",correct);
    fprintf (out,"Questions Wrong: %d \n",wrong);
    fprintf (out,"Questions not attempted: %d \n",notAttempted);

    system("pause");
    return 0;
}

Upvotes: 0

Views: 525

Answers (1)

ojblass
ojblass

Reputation: 21620

Your code

fscanf(in,"%s",studentAnswer);

is not taking into account the id before the students' answers. If the input file really does have '(' and ')' you must also account for skipping them. You must also somehow compare the id given to the line you are on. You have to compare the id you got in a while loop until you can read that students answers.

Upvotes: 1

Related Questions