User can bypass if/else by spamming letter

I've finished this game, my first one. It's just a simple one, but it works perfectly fine, at least for my first game.

The only problem I can't figure out how to fix is, if a user spams the known answers in one input (e.g "abbabba" then it skips the whole six other questions and gets to the score.

Is there a function for to stop this or should my code have been created differently?

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  #include <iostream>
using namespace std;

int main()
{
    cout << "                               QuizGame v1.0" << endl;

    int score = 0;
    char answera = 'a';
    char answerb = 'b';
    char playerInput;

    cout << "\nPlease enter your name.\n\n";

    string playerName; //Variable string for Player's name

    cin >> playerName; //Allow user input

    cout << "\n\nWelcome, " << playerName << "!" << endl
         << endl; //Welcome variable playerName

    cout << "First Question: What is the factor 'h' in Quantum mechanics?\n\n"
         << endl
         << "a. Planck Constant"
         << endl
         << "b. Superposition"
         << endl
         << endl;

    cin >> playerInput;

    if (playerInput == answera) {
        cout << "\n\nCorrect. 10 points!\n";
        score += 10;
    }

    else if (playerInput != answera) {
        cout << "\n\nIncorrect. Minus 5 points!\n";
        score -= 5;
    }

    cout << "\n\nSecond Question: What is the 'Photoelectric effect'?\n\n"
         << endl
         << "a. Discharge of electrons when light shines upon material"
         << endl
         << "b. Production of electrons when light shines upon material"
         << endl
         << endl;

    cin >> playerInput;

    if (playerInput == answerb) {
        cout << "\n\nCorrect! 10 points.\n";
        score += 10;
    }

    else if (playerInput == answera) {
        cout << "\n\nIncorrect. Minus 5 points.\n\n";
        score -= 5;
    }

    cout << "\n\nThird Question: What is Quantum mechanics?\n\n"
         << endl
         << "a. ... matter and energy on a scale familiar to human experience"
         << endl
         << "b. ... the behaviour of matter and its interactions with energy"
         << endl
         << endl;

    cin >> playerInput;

    if (playerInput == answerb) {
        cout << "\n\nCorrect! 10 points.\n";
        score += 10;
    }

    else if (playerInput == answera) {
        cout << "\n\nIncorrect. Minus 5 points.\n";
        score -= 5;
    }
    cout << "\n\nFourth Question: When did Quantum mechanics gradually rise?\n\n"
         << endl
         << "a. 1850s"
         << endl
         << "b. 1900s"
         << endl
         << endl;

    cin >> playerInput;

    if (playerInput == answerb) {
        cout << "\n\nCorrect! 10 points\n";
        score += 10;
    }

    else if (playerInput == answera) {
        cout << "\n\nIncorrect. Minus 5 points\n";
        score -= 5;
    }
    cout << "\n\nFifth Question: Who developed the modern concept of the Photon? \n\n"
         << endl
         << "a. Albert Einstein"
         << endl
         << "b. Robert Hooke"
         << endl
         << endl;

    cin >> playerInput;

    if (playerInput == answera) {
        cout << "\n\nCorrect! 10 points.\n";
        score += 10;
    }

    else if (playerInput == answerb) {
        cout << "\n\nIncorrect. Minus 5 points.\n";
        score -= 5;
    }

    cout << "\n\nSixth Question: What are 'Cathode rays'\n\n"
         << endl
         << "a. Elementary particles"
         << endl
         << "b. Streams of electrons"
         << endl
         << endl;

    cin >> playerInput;

    if (playerInput == answerb) {
        cout << "\n\nCorrect! 10 points\n";
        score += 10;
    }

    else if (playerInput == answera) {
        cout << "\n\nIncorrect. Minus 5 points.\n";
        score -= 5;
    }
    cout << "\n\nSeventh Question: What nationality was Niels Bohr?\n"
         << endl
         << "a. Danish"
         << endl
         << "b. Italian"
         << endl
         << endl;

    cin >> playerInput;

    if (playerInput == answera) {
        cout << "\n\nCorrect! 10 points.\n";
        score += 10;
    }

    else if (playerInput == answerb) {
        cout << "\n\nIncorrect. Minus 5 points.\n";
        score -= 5;
    }

    cout << "\nPlease type 's' to see your score.\n" << endl;
    char getScore = 's';

    cin >> playerInput;

    if (playerInput == 's') {
        cout << "\n\nYour Score: "
             << score
             << endl;
    }

    else {
        cout << "Invalid command, closing.";
        return 1;
    }

    if (score > 50) {
        cout << "\n\nYou win! Congratulations, " << playerName << "!\n" << endl;
    }

    else {
        cout << "\n\nYou lost! Apologies, " << playerName << ".\n" << endl;
    }
    cout << "Thanks for playing QuizGame!\n";

    cin >> playerInput;
    cout << "\n\n";
    return 0;
}
Your problem arises from the fact that you doing a cin do a single char (player Input). The extra characters remain in the input buffer waiting for the next cin. You have a couple of choices:
- cin.ignore () to remove any extra characters from the input buffer.
http://www.cplusplus.com/reference/istream/istream/ignore/
- Use a std::string for playerInput. Whatever the user typed will be placed in the string. If the user types extra characters, that won't match answera or answerb.

Some other comments:
You have a lot of repeated code in your program.
Lines 30-40 are the same for every question, except for the expected answer. Consider using a function and passing the expected answer as an argument to that function.

Lines 30-40: If the user enters something other than 'a' or 'b', you ignore the invalid input and proceed to the next question. You should prompt the user to enter 'a' or 'b' only before checking the answer.

Lines 9-10: answera and answerb should be const
Thank you for the reply and critique! While I don't fully understand what do to for the repeating code problem yet, I'm going to write some more programs using these to get used to them and learn more.

Hopefully I can get cin.ignore() working or std::string to not throw an error. But I'll keep going.

I know how annoying it can be to be bombarded with questions from someone who won't try, plus it's part of the learning experience.

Anyways, thanks again.
Topic archived. No new replies allowed.