0

I have an assignment to produce a program that will compare students answer to the answer key, and display the incorrect answers. The program then produces a report of the students incorrect answers and his final grade. The Program must use arrays and functions.

Currently I am trying to code two functions one to read the students answer file and store it in an array and the other to read answer key file and store it in another array. Then the functions will return both arrays to the main function later to be sent to another function to compare their contents(not yet done).

My problem with this code after pressing F11 to compile and run, i get a blank execution screen and a notification saying that the program has stopped working.

If my code contains a mistake or my approach is incorrect please tell me how to fix it. note: this is my first semester learning C programming.

Thank you.

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

//Modules
char* readstudent()
{
    FILE*s_ans;
    int i,j;
    static char arrs[20];
    s_ans=fopen("trial2.txt","r");

    if (s_ans == NULL)//check if file can be opened
    {
    printf("error student");
    }

    while(!feof(s_ans))
    {
        for(j=0;j<20;j++)
        {
            fscanf(s_ans,"%s",arrs[j]);
        }
    }
    printf("ReadStudent\n");
    for(i=0;i<20;i++)
    {
        printf("%d\t %s\n",i+1,arrs[i]);
    }

    return arrs;
}

char* readcorrect()
{
    FILE*c_ans;
    int x,i;
    static char arrc[20];   
    c_ans=fopen("CorrectAnswers.txt","r");

    if (c_ans == NULL)//check if file can be opened
    {
    printf("error correct");
    }

    while(!feof(c_ans))
    {
        for(x=0;x<20;x++)
        {
            fscanf(c_ans,"%s",arrc[x]);
        }
    }
    printf("ReadCorrect\n");
    for(i=0;i<20;i++)
    {
        printf("%d\t %s\n",i+1,arrc[i]);
    }

    return arrc;
}

//Main
int main()
{
    int i,j,n,x;
    char* as_ans=readstudent();
    char* ac_ans=readcorrect();

    printf("Main");
    for(i=0;i<20;i++)
    {
        printf("%s",as_ans[i]);
    }

    return 0;
}
  • 1
    What error are you getting? Post as much detail as possible for the community to assist you. Have a look at https://stackoverflow.com/help/how-to-ask if you are unclear. – Morne Dec 08 '19 at 07:04
  • Did you see all the warnings the compiler gave you? Fix those first or ask about them if you don't understand what they mean. – kaylum Dec 08 '19 at 07:14
  • `static char arrs[20]; fscanf(s_ans,"%s",arrs[j]);`. The compiler should be warning you about this code and multiple other lines with similar errors. You are passing a single `char` where a `char *` is needed. – kaylum Dec 08 '19 at 07:15
  • The code compiles successfully with zero errors. _kaylum – Marwan Deabes Dec 08 '19 at 07:19
  • Not errors. Warnings. Did the compiler have any messages at all? If there are no warnings you may want to turn up the warning level on your compiler. Anyway, I pointed out the type of errors you have. – kaylum Dec 08 '19 at 07:21
  • So you've got 1 array "arrs" of 20 chars defined in readstudent but then you read the file s_ans for string you put into arrs[j] , you read for 20 strings not for 20 chars, why not use just once fgets ? And if you expect 20 chars define arrs[21] not 20. – BobRun Dec 08 '19 at 07:23
  • the compiler doesn't give any messages, can you further explain how to solve the error you see? @kaylum – Marwan Deabes Dec 08 '19 at 07:41
  • Get a better compiler:( – Martin James Dec 08 '19 at 10:33
  • its Dev C++, its predetermined by the course. :( – Marwan Deabes Dec 08 '19 at 11:19
  • When compiling, always enable the warnings (this can be done in `Dev C++`) then fix those warnings. When I compiled the program with "gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" the compiler output 9 warning message, several of which are critical. Like passing an `int` when a `char *` is expected. – user3629249 Dec 09 '19 at 07:07
  • [to display the compiler warning/error window](https://stackoverflow.com/questions/25704270/no-error-window-in-dev-cpp) – user3629249 Dec 09 '19 at 07:12
  • click`tools` then in the drop down menu click `compiler options` then in the tabs in the resulting window click `warnings` and enable the warnings. – user3629249 Dec 09 '19 at 07:18
  • Per the sourceforge page on Dev C++, the project has been suspended for several years, so does not support beyond C++11 – user3629249 Dec 09 '19 at 07:28

0 Answers0