I’ve been working on this project for a while, it’s a new language for me but I had a partner with experience who kinda ditched me. Anyways, right now I’m having trouble with getting the text from one object to another. I instantiate an object in the game class, then try to get it and save it to another object in the main class but when I get the object it’s empty! I don’t know what’s going on here and I just can’t seem to figure it out.
but the part that isn’t working is in the display method when I try to draw the questions text:
drawText((WinWidth/2)-225, (WinHeight/2) - 90, curQuestion.question.c_str());
curQuestion is created at the top but instantiated in the mouse method:
curQuestion = g.getQuestion(col,row);
and here’s the game class (which is in Cc.h)
class Game {
public:
Game(bool);
void initQuestions();
Question getQuestion(int, int);
string getQuestionText(int, int);
private:
Question questions[5][5];
};
Game::Game(bool m)
{
mp = m;
initQuestions();
}
void Game::initQuestions()
{
bool hasDouble = false;
srand( time(NULL));
int blarg = rand() % 25 + 1;
fstream questionFile;
questionFile.open("questions.txt", ifstream::in);
int cur = 0;
for(int c = 0; c < 5; c++)
{
for(int r = 0; r < 5; r++)
{
char * q = new char[256];
char * a = new char[256];
questionFile.getline(q,256);
questionFile.getline(a,256);
questions[c][r] = Question(c,r, false, q, a);
cout << questions[c][r].question.c_str() << questions[c][r].answer.c_str();
}
}
questionFile.close();
}
Question Game::getQuestion(int c, int r)
{
return questions[c][r];
}
string Game::getQuestionText(int c, int r)
{
return questions[c][r].question;
}
Note: the cout called in the game method does return exactly what it should!
Question class:
class Question {
public:
int col;
int row;
bool dailyDouble;
string question;
string answer;
int value;
Question();
Question(int, int, bool, string, string);
bool checkAnswer(string);
string getQuestion();
};
Question::Question() { }
Question::Question(int c, int r, bool d,string q, string a)
{
col = c; row = r; dailyDouble = d; question = q, answer = a;
cout << "TEST> Q: " << question << ", A: " << answer << endl;
if(d)
value = r * 200 * 2;
else
value = r * 200;
}
bool Question::checkAnswer(string answer)
{
if(answer.find("What is") && answer.find(answer))
return true;
return false;
}
string Question::getQuestion() {
return question;
}
I really can’t understand what’s going wrong here, any help is greatly appreciated. I hope that once I figure out what’s going wrong here I’ll be able to finish on my own!
I tried your code on my computer, it seems that both the Question class and the Game class is correct, I also tried the Game::getQuestion() and the Game::getQuestionText() method, it’s all correct, and returns the proper value.
By the way, there are no pointer members in the Question Class and the Game Class, so there is no need to write a copy constructor and a operator=() overload method.
Maybe you can check if the col and row you passed in g.getQuestion(col,row) is right or not.
Sorry have to post an answer here, cause I don’t know how to add a comment to your question.
wish this will help.