这是一个家庭作业。我遇到了一个奇怪的错误,我不知道是什么原因造成的。这不是全部任务,但它是我目前为止的任务。由于这是家庭作业,我不希望有人确切地告诉我如何编码,而是想知道是什么导致了我的崩溃以及我如何能够开始寻找如何解决它。一旦我的程序在 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);
}