Using: MSVS2012
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
#includes etc
ElemAlg *ea;
ea->GetQuiz(1);
GetQuiz
is definitely passed an integer between 1 and 4, this is verified before the method is called
difficultyLevel
is a string defined in the header file.
The compiler throws an Unhandled exception and Access violation writing location ... as soon as it hits the first if
function.
If I remove the if
functions and define difficultyLevel
as algE just for testing the same problem.
If I remove difficultyLevel
entirely and just open the file as "algE.txt"
and "algEAnswers"
then I get the same problem but at a different memory location once the code hits the while loop.