【问题标题】:Unhandled exception, orgin unknown未处理的异常,来源未知
【发布时间】:2013-10-13 09:31:29
【问题描述】:

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

【问题讨论】:

  • 这是一个 c++ 介绍类,我们可以在其中混合使用 c 和 c++。我非常缺乏经验,并认为我的实现主要是 c,但只要它在 Visual Studio 2010 中编译并产生正确的输出,任何一种都是允许的。
  • 我认为,string correctAnswersfscanf ... %s 类型不匹配。 fscanf 可能会收到 char*
  • (这不会解决你的问题)不要为了它而传递指针。例如,在 printData(..) 中,您传递一个指向字符串的指针。相反,更改 printData 的签名以接受对常量字符串的引用) void printData(..., const string& correctAnswers, ...)

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


【解决方案1】:

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

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 表示发现的错误,但我不确定“学习​​使用调试器”是否是一个好建议。让错误出现并在以后删除它们的想法一般来说效果不佳,尤其是在具有 UB 的语言中。
  • @6502 好的,我已经稍微调整了我的语言。
【解决方案2】:

由于您使用的是 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

【讨论】:

  • 你知道你可以说using std::cin;using std::cout;。说using namespace std; 是在为任何超过几行的代码自找麻烦。
  • 谢谢,我已经熟悉 cin 和 cout 的基本用法以及标准命名空间。我对文件读写不太了解……在iostream中是否有类似于fscanf的选项?我最初决定在这个项目中使用 printf 和 scanf,因为我认为它们与 fscanf 一起使用。我没有意识到我的字符串是 c++ 风格的字符串,这实际上解释了很多。为什么在崩溃之前我的所有输出仍然正确?例如 printf("%d\t%s\n", numOfQuestions, *p_correctAnswers);正确输出 5[TAB]dbbac,这正是我从考试中读到的?
  • 是的 - 已编辑。是的,这就是 C++ 的美妙之处。您的文件只是另一个“流”。打开后,您可以使用 >> 和 cplusplus.com/reference/istream/istream/operator%3E%3E
  • 关于失败,您的错误消息表明它是一个写入问题。 printf 似乎工作(奇怪) - 我认为这是因为您指向字符串的指针与 .c_str() 将生成的指针相同,这意味着字符存储在字符串容器的最开头。
  • 我正在为这个程序转换为完全使用 C++,但我不确定如何像使用 scanf 那样一次读取多个内容。例如我怎么写 scanf("%d %s", &p_studentIds[i], p_studentAnswers[i]);作为 cin 声明? cin
猜你喜欢
  • 2014-02-11
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多