Jump to content
  • Advertisement
Sign in to follow this  
Hudsucker72

newbie quiz help

This topic is 5045 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, i start by greeting all of you, as this is my first time here. I have just started c++ and i'm trying make a quiz for pratice. I want the quiz to be multiple choice, a b and c. I'd also like the questions to be random, so not always the same order. What kind of structure should i start with? I've been studying for a few days, but it's still all a big blur for the moment. Don't know where to start or what method I should go with. I thought about using functions for each questions and randomly calling one. Does that make any sense? Any help would be appreciated. Thanks

Share this post


Link to post
Share on other sites
Advertisement
Okay, if you've got to classes already in your learning then you could try this, if not i'd recomend you keep playing around with the examples in your book/course until you do.

You can creat a class to reresent a question something like this

class Question
{
public:
// I'll leave you to fill in the code for these methods,
// after all it's half the fun!

// Constructs a new question with mQuestion set
Question( std:string questionText );

// Adds a possible answer, the boolean is used to determine
// whether this is the correct answer or a fake one
// it should check that there is not already a correct answer
// if this boolean is true.
void AddAnswer( std::string answer , bool correct );

// Displays the question in a random order
// you may need to store the order so that you
// can validate the users answer later
// You may need to add a parameter so the question
// knows where to display the question ( it could be the
// console , a window , or anywhere )
void ShowQuestion();

// Checks to see if a user input is correct
// Will need to reference the stored values in showquestion
bool IsCorrect( int userGuess );


private:
std::string mQuestion;
std::vector<std::string> mAnswers;

int mCorrectAnswerIndex;
};

When you start your program you can create a bunch of these ...
Question q = new Question("What is 2+2?");
q.AddAnswer( "6" , false );
q.AddAnswer( "4" , true );

... and add them to some global array or vector of questions..

std:vector<Question> globalQuestions;

...

globalQuestions.push_back( q );

...

Then , when you've set up plenty of questions ( you could load them from an xml file for example ) your main loop looks like this

while 1
{
int i = rand() % globalQuestions.Count(); // i think it's count
// but i've been
// using .net collections recently

globalQuestions.ShowQuestion();

// get user input, use cin maybe
int a;
cin >> a;

if ( globalQuestions.IsCorrect( a ) )
score++;
}

Of course you'd not use a while 1 loop and you'd have to keep track of which questions had already been asked etc. But you should have enough to keep you going. Welcom to C++

Share this post


Link to post
Share on other sites


#include <iostream>
using namespace std;

int main()
{
const int NUMBER_OF_QUESTIONS = 3;

char *questions[NUMBER_OF_QUESTIONS][4] =
{ "How many in a dozen?",
"a. 9",
"b. 10",
"c. 12",

"What is 2 + 2?",
"a. 2",
"b. 5",
"c. 4",

"How many inches in a foot?",
"a. 36",
"b. 18",
"c. 12"};

char answers[] = { 'c', 'c', 'c' };

char answer;

for(int i =0; i < NUMBER_OF_QUESTIONS; i++)
{
cout << "\nQuestion " << i+1;
for(int j = 0; j < 4; j++)
{
cout << "\n" << questions[j];
}

cout << "\nAnswer: ";
cin >> answer;

if (answer == answers)
cout << "Correct!" << endl;
else
cout << "Sorry." << endl;
}

return 0;
}




That's how I would do it. Then you can just add some rand() calls to mix up the order of the questions (instead of just going through them like I did)

Hope it helps!

- heap

Edit: You should know I'm new to C++ as well. I'm pretty good with C though! I'm glad Keem presented an OOP solution as well ;)

Share this post


Link to post
Share on other sites
Thanks a lot for the quick answer guys. The classes are the next thing in my book, so i'll study your examples. Looks good though, even if i read this as fast as I read Chinese right now :)

Share this post


Link to post
Share on other sites
Always glad to help. Really, these are amongst my favorite types of posts because they give me the opportunity to try and solve a new problem. I often have trouble coming up with them on my own!

Just out of curiosity, what book are you using? I'm using C++ Primer Plus, which so far seems pretty good. A lot of side notes for C programmers, which is a plus for me.

- heap

Share this post


Link to post
Share on other sites
Listen to Keem, the design there is pretty good. Of course, you'll have to figure out how to write the methods.

Share this post


Link to post
Share on other sites
I'm reading Stroustrup "The C++ programming language". But I think it's a bit advanced for my taste. Still lots of grey areas in my understanding.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!