未处理的异常,原始未知
Unhandled exception, orgin unknown
这是一个家庭作业。我遇到了一个奇怪的错误,我不知道是什么原因造成的。这不是全部任务,但这是我目前所拥有的。由于这是家庭作业,我不希望有人确切地告诉我如何编码它,而是我想知道是什么导致了我的崩溃,以及我如何能够开始寻找如何解决它。我的程序一旦命中 main 方法中的 return 语句,它似乎就会崩溃。所有输出似乎正是我试图获得的,并且我的文件正在正常读取。打印完所有信息后按回车键后,我会看到一个消息框,上面写着"考试中0x5286ad84时未处理的异常 (msvcp100d.dll.exe: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.nn");
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);
}
可能会有很多错误,使用调试器将帮助您跟踪它们。但我发现的两个是
printf("%dt%sn", numOfQuestions, *p_correctAnswers);
不能将C++字符串与 printf 一起使用,只能使用 C 字符串。这将起作用
printf("%dt%sn", numOfQuestions, p_correctAnswers->c_str());
c_str 方法从C++样式字符串返回 C 样式字符串。
还有另一个类似的错误
int result = fscanf(p_inputf, "%d %s", &p_studentIds[i], p_studentAnswers[i]);
不能将C++字符串与 FSNF 一起使用。这个问题很难解决。由于你想使用C++字符串(这是一个聪明的举动),我认为你也应该学习一些C++I/O,查找iostreams。
由于您使用的是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的信息
相关文章:
- 继承期间显示未知行为的子类
- 输入中的字符串数未知(以字母表示)
- 具有未知值时的时间复杂性
- 将浮动的heightmap数组导出为16位原始值
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 有没有办法从非C/C++文件中读取C++原始字符串文字的内容
- 链表中写入访问冲突的未知原因
- 正在生成未知类实例
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 是否可以将llvm::FunctionType转换为C/C++原始函数指针
- 读取文件时运行时的未知行为
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 初始化多个未知基类
- 如何将原始字节附加到 std::vector?
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- 从堆栈分配的原始指针构造智能指针
- 如何重写全局方法名称以在调用原始方法之前将我的代码推到前面
- 将unique_ptr分配给原始指针
- 如何存储未知次数迭代的输入?
- 未处理的异常,原始未知