未处理的异常,orgin未知

时间:2013-10-13 09:31:29

标签: c++ runtime-error unhandled-exception

这是一项家庭作业。我遇到了一个奇怪的错误,我不知道是什么导致它。这不是整个任务,但它是我迄今为止所拥有的。由于它的功课,我不希望有人告诉我如何编码它,而是我想知道是什么导致我的崩溃以及我如何能够开始寻找如何解决它。一旦遇到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);
}

2 个答案:

答案 0 :(得分:4)

可能存在很多错误,使用调试器可以帮助您跟踪它们。但我发现的两个是

printf("%d\t%s\n", numOfQuestions, *p_correctAnswers);

您不能将C ++字符串与printf一起使用,只能使用C字符串。这将有效

printf("%d\t%s\n", numOfQuestions, p_correctAnswers->c_str());

c_str方法从C ++样式字符串返回C样式字符串。

另一个类似的错误

int result = fscanf(p_inputf, "%d %s", &p_studentIds[i], p_studentAnswers[i]);

你不能在fscanf中使用C ++字符串。这个很难解决。既然你想使用C ++字符串(这是一个聪明的举动)我认为你也应该学习一些C ++ I / O,查找 iostreams

答案 1 :(得分:0)

由于你使用的是C ++编译器,你可以撤消一些C习惯(越早越好!)

#include <iostream> // for std::cout and std::cin

using namespace std::cin; 
using namespace std::cout;
using namespace std::endl;

使用:

代替printf(以及john指出的关于C样式字符串的问题)
cout << numOfQuestions << "\t" << correctAnswers << endl;

您可以阅读有关cout here

的信息