1

これは宿題です。奇妙なエラーが発生しましたが、何が原因なのかわかりません。これはすべての割り当てではありませんが、これまでのところ私が持っているものです。宿題なので、誰かに正確なコーディング方法を教えてもらいたくありません。むしろ、クラッシュの原因と、それを解決する方法を探し始める方法を知りたいです。main メソッドの return ステートメントに到達すると、プログラムがクラッシュするようです。すべての出力はまさに私が取得しようとしていたものであり、私のファイルは問題なく読み取られています。すべての情報が出力された後に Enter キーを押すと、「Exams.exe の 0x5286ad84 (msvcp100d.dll) で未処理の例外: 0xC0000005: アクセス違反の書き込み場所 0x00000063」というメッセージ ボックスが表示されます。誰でも提供できるヘルプをありがとう。

#include <stdio.h>
#include <string>
#define MAX_STUDENTS 100
#define MAX_QUESTIONS 50
using namespace std;

int fGetAnswers(FILE *p_inputf, int p_studentIds[], string p_studentAnswers[]);
int fGetAnswers(int p_studentIds[], string p_studentAnswers[]);
void printData(int numOfStudents, int numOfQuestions, string *p_correctAnswers, int studentIds[], string studentAnswers[]);

int main(void)
{
    // Declarations
    FILE *inputf = fopen("examdat.txt", "r"); // Opens "examdat.txt" and creates a pointer to it.
    int studentIds[MAX_STUDENTS];
    int *p_ids = studentIds; // Pointer to the studentIds array to pass to functions for manipulation.
    string studentAnswers[MAX_QUESTIONS];
    string *p_answers = studentAnswers; // Pointer to the studentAnswers array to pass to functions for manipulation.
    int numOfQuestions;
    string correctAnswers;
    int numOfStudents;
    fscanf(inputf, "%d %s", &numOfQuestions, correctAnswers); // Fetches the first line from the exam.dat file which contains the number of questions and the correct answers.

    int readFrom = 0;
    while (readFrom != 1 && readFrom != 2) // Loops until proper input is received. Asks if the user wants to read student data from the text file or enter it manually.
    {
        printf("1. Read student data from file.\n");
        printf("2. Enter student data manually.\n\n");
        printf("Please make a selection> ");
        scanf("%d", &readFrom);
        if (readFrom != 1 && readFrom != 2)
            printf("\nInvalid entry!\n");
    }
    if (readFrom == 1) // Calls fGetAnswers to retrieve answers from the text file.
        numOfStudents = fGetAnswers(inputf, p_ids, p_answers);
    else // Calls fGetAnswers to retrieve answers from the user via the keyboard.
        numOfStudents = fGetAnswers(p_ids, p_answers);

    fclose(inputf);

    printData(numOfStudents, numOfQuestions, &correctAnswers, studentIds, studentAnswers);

    getchar();
    getchar();
    return(0);
}
4

2 に答える 2