Usage: MSVS2012
the code
elemalg.h
#include <vector> #include <string> #include <fstream> class ElemAlg { private: std::string difficultlyLevel, question, answerToRead; std::vector<std::string> questions, answers; std::vector<std::string> GetQuiz(int); };
elemalg.cpp
#include "elemalg.h" std::vector<std::string> ElemAlg::GetQuiz(int difficulty) { if (difficulty == 1) { difficultyLevel = "algE"; } if (difficulty == 2) { difficultyLevel = "algM"; } if (difficulty == 3) { difficultyLevel = "algH"; } if (difficulty == 4) { difficultyLevel = "algVH"; } std::ifstream fin(difficultyLevel + ".txt"); while (std::getline(fin, question)) { questions.push_back(question); } fin.close(); std::ifstream fin2(difficultyLevel + "Answers.txt"); while (std::getline(fin2, answerToRead)) { answers.push_back(answerToRead); } fin2.close(); return questions; }
MathTutor.cpp
GetQuiz definitely passed an integer from 1 to 4, this is checked before the method is called
difficultyLevel is the string defined in the header file.
The compiler throws the error message "Unhandled Exception" and "Access Violation" ... as soon as it gets into the first if function.
If I remove the if functions and define difficultyLevel as algE just to test the same problem.
If I completely remove difficultyLevel and just open the file as "algE.txt" and "algEAnswers" , then I get the same problem, but in a different memory location, when the code gets into the while loop.
source share