TryBugHunt
cppLogic errorEasy

Unregistered 30-year-old allowed to vote

BugFind the bug and fix the code

What it should do

canVote(age, registered) is true only for people who are at least 18 and registered. Example: canVote(20, true) → true

What happens instead

canVote(30, false) returns true, and so does canVote(16, true).

bool canVote(int age, bool registered) {
    return age >= 18 || registered;
}

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the logic errors pattern

The wrong operator, an inverted condition, a formula the right way round but backwards.

What you'll practise

An easy C++ debugging exercise about logic errors: the wrong operator, an inverted condition, a formula the right way round but backwards.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

What went wrong: || is true when either side is true, so being old enough or being registered is treated as enough on its own.

The fix: Both rules must hold, so join them with &&.

Remember: Read the rule out loud. "Must be 18 and must be registered" is &&. "Can pay by card or by cash" is ||. Test one case where only the first condition holds and one where only the second holds.

The pattern behind it: logic errors.

More logic errors to practise

Embed this challenge on your site

Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.

<iframe src="https://trybughunt.com/embed/eb82c4e7-7058-4196-8ba6-1b1e96c8dda6" title="Debugging challenge: Unregistered 30-year-old allowed to vote" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/eb82c4e7-7058-4196-8ba6-1b1e96c8dda6">TryBugHunt</a></p>