this is my first post here so please forgive me if the etiquette is poor. I figured this is also a good time for me to learn how to use this tool properly. Thanks guys/gals.
Anyways, I am working on a trivia game for a school project. I am reading in questions and answers from a CSV file that will be displayed to the user through the terminal. But I am having trouble inserting my nodes into the array.
My tactic here is to read in question first then read the correct answer in second, and the last three answers are the incorrect answers which will be shuffled later.
ifstream questionFile("questions.csv");
getline(questionFile, header);
if(questionFile.is_open())
{
string line;
while(getline(questionFile, line))
{
stringstream ss;
ss << line;
string item;
getline(ss, item, ',');
question = item;
getline(ss, item, ',');
answer1 = item;
getline(ss, item, ',');
answer2 = item;
getline(ss, item, ',');
answer3 = item;
getline(ss, item, ',');
answer4 = item;
g.insertAndShuffle(question, answer1, answer2, answer3, answer4);
}
}
questionFile.close()
I then pass these values into a function which will assign the answer strings into a node which will also hold a boolean value (correct answer holds true). Here's my issue I think... I then pass pointers to these nodes along with the question into an array[5] so that I can shuffle array[1]-[5] to randomize the answers.
Here is the function:
string* Game::insertAndShuffle(string question, string answer1, string answer2, string answer3, string answer4)
{
Answer *nodeC = new Answer(answer1, true);
Answer *node1 = new Answer(answer2, false);
Answer *node2 = new Answer(answer3, false);
Answer *node3 = new Answer(answer4, false);
quizArray[0] = question;
quizArray[1] = nodeC;
quizArray[2] = node1;
quizArray[3] = node2;
quizArray[4] = node3;
random_shuffle(&quizArray[1], &quizArray[5]);
return quizArray;
}
The class containing the function:
class Game
{
public:
bool isAsked;
string quizArray[5];
int questionNum = 1;
bool letsPlay = false;
string* insertAndShuffle(string question, string answer1, string answer2, string answer3, string answer4);
string askQuestions(string* quizArray);
bool startGame(bool letsPlay, string playerName);
// bool checkAnswer(string playerResponse, string answer1);
private:
Answer *nodeC;
Answer *node1;
Answer *node2;
Answer *node3;
};
And the struct being used for answers:
struct Answer
{
string answer;
bool isCorrect;
Answer();
Answer(string item, bool value)
{
answer = item;
isCorrect = value;
}
};
There seems to be a problem when I am trying to access the the pointer to the struct through the class function. Here is my error:
./project.hpp:22:2: error: unknown type name 'Answer'
Answer *nodeC;
^
./project.hpp:23:2: error: unknown type name 'Answer'
Answer *node1;
^
./project.hpp:24:2: error: unknown type name 'Answer'
Answer *node2;
^
./project.hpp:25:2: error: unknown type name 'Answer'
Answer *node3;
^
./project.hpp:43:2: error: C++ requires a type specifier for all declarations
CorrectAnswer();
^
./project.hpp:44:2: error: C++ requires a type specifier for all declarations
CorrectAnswer(string item, bool value)
^
In file included from projectMain.cpp:8:
./project.cpp:85:22: error: no matching constructor for initialization of 'Answer'
Answer *nodeC = new Answer(answer1, true);
Thanks for any feed back, I hope I am doing this right.
Aucun commentaire:
Enregistrer un commentaire