HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

C++ quiz game with 25 questions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withquizgamequestions

Problem

I created a quiz game program with 25 questions, 4 answer choices, correct answer and 4 points awarded per question. I realize this code isn't the best; I am still a beginner and this is my first major project. I am open to criticism and any suggestions on how to shorten this code (especially where the questions are typed with their answers and correct answer).

```
#include
#include

char guess; //Answer user inputs for question.
int total; //Total score.

//4 possible answers, correct answer and question score.
class Question{
public:
void setValues(std::string, std::string, std::string, std::string, std::string, char, int);
void askQuestion();

private:
std::string Question_Text;
std::string answer_1;
std::string answer_2;
std::string answer_3;
std::string answer_4;

char correct_answer;
int Question_Score;
};

int main()
{
//Program Title designed with an ASCII art generator.
//Link: http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something%20
std::cout > name;
std::cout > respond;

//If user says yes, the quiz begins.
if (respond == "Yes" || respond == "yes") {
std::cout >",
"cin ",
"cin >>",
'd',
4);

q4.setValues("4. What is this called, ?",
"directive",
"pre-processor directive",
"file",
"command",
'b',
4);

q5.setValues("5. What punctuation ends most lines of code?",
" . ",
" ; ",
" : ",
" ' ",
'b',
4);

q6.setValues("6. Which of the following is a correct comment?",
"/ Comments /",
" Comment ",
"/ Comment /",
"{ Comment }",
'c',
4);

q7.setValues("7. Which of the following is the boolean operator for logical-and?",
"&",
"|",
"&&",
"|&",
'c',
4);

q8.setValues("8. Which of the following shows the correct syntax for an if

Solution

Hi and welcome to code review. This already looks quite nice for a beginner, here are some initial thoughts.

When creating a nontrivial class always provide a constructor. That way there is no chance that your class can be used improperly. In your case this would amount to:

explicit Question(const std::string& Question, 
                  const std::vector& Answers);


Also I would group things together that belong together, e.g. the answers

std::vector answers;


But this also applies for the individual questions themselves.

As you already created a class for the questions, also do so for the players. Again with constructor etc.

Put more functionality into separate functions. This helps to structure your code better. In the end, the game itself should be a class with a player, a vector of questions and the score as members and functionality for the input output.

Rather than asking the questions in the same order every time you can mix them up using std::random_shuffle.

Code Snippets

explicit Question(const std::string& Question, 
                  const std::vector<std::string>& Answers);
std::vector<std::string> answers;

Context

StackExchange Code Review Q#147618, answer score: 8

Revisions (0)

No revisions yet.