[REQUEST]Code Review

Hello, I've created code for a quiz game over the C++ language. It works fine and everything. However, I feel like the code could use improvement. Specifically with all the repetition of typing all these questions and answers. Maybe even changing this program to generate these questions in random order rather than in the order I have them in now.

Code: http://pastebin.com/YAcudX7T
Have you studied vectors/arrays yet?

Have you considered using a constructor to "set" the values?

Have you considered using something to provide case insensitive answers?

IMO you have to press enter too many times.

I have not studied vectors but I have briefly studied arrays a bit.

I have thought about both of those things, how should I go about with using a constructor instead of the setValues function?
I have not studied vectors but I have briefly studied arrays a bit.

Maybe it's time you took a look at std::vector, anytime I see a variable with a number tacked on the end I think of std::vector.

how should I go about with using a constructor instead of the setValues function?

Create a constructor with the proper parameters and use that constructor instead. By the way if you do create a constructor don't forget that the "default no argument" constructor will no longer be provided by the compiler you may need to also create that constructor as well.

See this link for more information:

http://en.cppreference.com/w/cpp/language/initializer_list

Last edited on
Suggestions. Use arrays (or better vectors) and loops. Separate data from program logic. Move the ASCII art into its own small function so it doesn't clutter the main code. Perhaps put the questions and answers into a text file, (or even a stringstream inside the program) and load a vector of Questions from the file or stream. Avoid global variables.

Quick example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>

const int s_questionScore = 4;  // Points rewarded for each correct answer.

class Question { 
public:
    void setValues(std::string, std::string, std::string, std::string, std::string, char);      
    int askQuestion(int num = -1);  
    friend std::istream& operator >> (std::istream& is, Question& ques);    

private:
    std::string Question_Text;
    std::string answer_1;
    std::string answer_2;
    std::string answer_3;
    std::string answer_4;
    char correct_answer;
};

void load(std::istream& is, std::vector<Question>& vques);
void load_sstream(std::istringstream & ss);

int main()
{   
//    std::ifstream fin("quiz_data.txt");
    std::istringstream fin;
    load_sstream(fin);    
        
    std::vector<Question> questions;
    load(fin, questions);
    
    std::cout << "Number of questions = " << questions.size() << '\n';
    
    int total = 0;
    
    for (size_t i=0; i<questions.size(); ++i)
    {
        total += questions[i].askQuestion(i+1);
        std::cout << "Total score = " << total << '\n';
    }
       
}

std::istream& operator >> (std::istream& is, Question& ques)
{
    std::string line;
    while (getline(is, line))
    {
        if (line.size() == 0)
            continue;
        break;
    }
    ques.Question_Text = line;
    getline(is, ques.answer_1);
    getline(is, ques.answer_2);
    getline(is, ques.answer_3);
    getline(is, ques.answer_4);
    is >> ques.correct_answer;
    return is;
}

void load(std::istream& is, std::vector<Question>& vques)
{
    Question q;
    while (is >> q)
        vques.push_back(q);
}

//Format for possible answers displayed when program executes. 
int Question::askQuestion(int num)
{
    int score = 0;
    // Ask the question.
    std::cout << "\n";
    if (num > 0)
        std::cout << num << ". ";
    std::cout << Question_Text << "\n";
    std::cout << "a. " << answer_1 << "\n";
    std::cout << "b. " << answer_2 << "\n";
    std::cout << "c. " << answer_3 << "\n";
    std::cout << "d. " << answer_4 << "\n";
    std::cout << "\n";

    char guess;
    //Ask user for their answer.
    std::cout << "What is your answer?" << "\n";
    std::cin >> guess;
    //If their answer is correct, message is displayed and 4 points are added to their score.
    if (guess == correct_answer) {
        std::cout << "\n";
        std::cout << "Correct!" << "\n";
        score = s_questionScore;
        std::cout << "\n";
        std::cout << "Press enter to continue." << "\n";
        std::cin.get();
        std::cin.ignore();
    }
    else //If their answer is incorrect, message is displayed, no points added. 
         //Correct answer displayed. 
    {
        std::cout << "\n";
        std::cout << "Sorry, you're wrong..." << "\n";
        std::cout << "The correct answer is " << correct_answer << "." << "\n";
        std::cout << "\n";
        std::cout << "Press enter to continue." << "\n";
        std::cin.get();
        std::cin.ignore();
    }
    
    return score;
}

void load_sstream(std::istringstream & ss)
{
    ss.str(R"(

What command prints something to the screen?
cin
cout
char
print
b

Which of the following categories does C++ belong to?
Operating System
High-level programming language
low-level programming language
Compiler
b

Which command is correctly written?
cout >>
cin <<
cout <>
cin >>
d

What is this called, <iostream>?
directive
pre-processor directive
file
command
b

   
)");

}


Lines 31 and 32 create a stringstream, Alternatively, you could omit those two lines and load the data from a text file (line 30).

Last edited on
Topic archived. No new replies allowed.