TryBugHunt
cppOtherMedium

Salary "1,200" is read as 1

BugFind the bug and fix the code

What it should do

parseSalary(text) reads an amount that may contain thousands commas. Example: parseSalary("1,200") → 1200

What happens instead

parseSalary("1,200") returns 1 instead of 1200.

#include <algorithm>
#include <string>

int parseSalary(std::string text) {
    return std::stoi(text);
}

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 mutation and copying bugs pattern

Copies where you meant references, references where you meant copies, and shallow copies of pointers.

What you'll practise

A medium C++ debugging exercise about mutation and copying bugs: copies where you meant references, references where you meant copies, and shallow copies of pointers.

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: std::stoi reads digits until it meets a character that is not part of a number, then stops without any error. In "1,200" it reads the 1, stops at the comma and returns 1.

The fix: Remove the commas before parsing: text.erase(std::remove(text.begin(), text.end(), ','), text.end());

Remember: stoi only complains when there is no number at the start. Anything after the first bad character is silently ignored, so formatted input (commas, spaces, currency signs) must be cleaned first.

The pattern behind it: mutation and copying bugs.

More mutation and copying bugs 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/2f3f9e7e-9e1f-4e4e-8184-ebc592fad512" title="Debugging challenge: Salary &quot;1,200&quot; is read as 1" 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/2f3f9e7e-9e1f-4e4e-8184-ebc592fad512">TryBugHunt</a></p>