-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
83 lines (67 loc) · 3.43 KB
/
Copy pathmain.cpp
File metadata and controls
83 lines (67 loc) · 3.43 KB
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
// main.cpp — Interactive multiplication quiz (CLI entry-point)
//
// Pedagogical intent: this file is deliberately simple and heavily commented
// so that stepping through it in the VS Code debugger is instructive.
//
// Suggested breakpoints when debugging for the first time:
// 1. The top of the game loop (while-loop header) to inspect `seed`.
// 2. The check_answer() call to watch `player_answer` vs `q.correct`.
// 3. score.record() (step into quiz_lib) to see the Score state change.
#include <iostream>
#include <limits>
#include <string>
#include "quiz.hpp" // quiz::generate_question, quiz::check_answer, quiz::Score
namespace {
/// Number of questions in one game session.
/// Kept small so a manual debug session doesn't feel tedious.
constexpr int kQuestionsPerGame = 5;
/// Read a single integer from stdin, discarding any leading whitespace.
/// Returns false (and leaves `out` unchanged) if the input is not a valid int.
bool read_int(int& out) {
int value{};
if (std::cin >> value) {
// Discard remainder of the line (newline, trailing garbage) so the
// next std::cin >> call starts on a fresh line.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
out = value;
return true;
}
// Clear the error flag and discard the bad token so the stream is reusable.
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return false;
}
} // anonymous namespace
int main() {
std::cout << "=== Multiplication Quiz ===\n"
<< "Answer " << kQuestionsPerGame << " multiplication questions.\n\n";
quiz::Score score;
// Each iteration of the loop is one question.
// `seed` drives generate_question(); incrementing it gives a new question
// each round while keeping the sequence deterministic (useful for demos).
for (int seed = 0; seed < kQuestionsPerGame; ++seed) {
// ── Generate question ─────────────────────────────────────────────
quiz::Question q = quiz::generate_question(seed);
// Print the question number (1-based for the player) and the prompt.
std::cout << "Q" << (seed + 1) << ": " << q.prompt();
// ── Read player answer ────────────────────────────────────────────
int player_answer{};
if (!read_int(player_answer)) {
std::cout << " [invalid input — skipping]\n";
score.record(false); // Count as incorrect
continue;
}
// ── Check answer and give feedback ────────────────────────────────
bool correct = quiz::check_answer(q, player_answer);
score.record(correct);
if (correct) {
std::cout << " Correct!\n";
} else {
std::cout << " Wrong. The answer was " << q.correct << ".\n";
}
}
// ── Final score ───────────────────────────────────────────────────────
std::cout << "\nResult: " << score.summary() << "\n";
// Return 0 on a perfect score, 1 otherwise — useful for scripting.
return (score.correct_count() == kQuestionsPerGame) ? 0 : 1;
}