TryBugHunt
cppType errorMedium

Cart total drops the cents

BugFind the bug and fix the code

What it should do

cartTotal(prices) adds up the prices in a cart. Example: cartTotal({1.5, 2.25}) → 3.75

What happens instead

cartTotal({1.5, 2.25}) returns 3 instead of 3.75.

#include <numeric>
#include <vector>

double cartTotal(std::vector<double> prices) {
    return std::accumulate(prices.begin(), prices.end(), 0);
}

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 type errors pattern

Integer division, signed and unsigned mix-ups, and implicit conversions that change the value.

What you'll practise

A medium C++ debugging exercise about type errors: integer division, signed and unsigned mix-ups, and implicit conversions that change the value.

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::accumulate keeps its running total in the type of the starting value. 0 is an int, so after each addition the result is stored back in an int and the decimals are cut off: 0 + 1.5 becomes 1, then 1 + 2.25 becomes 3.

The fix: Start with a double: 0.0.

Remember: The third argument of std::accumulate sets the type of the whole calculation. Use 0.0 for doubles and 0L for big integer sums.

The pattern behind it: type errors.

More type 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/8c7fd612-4266-4724-a9c4-97000b3c9925" title="Debugging challenge: Cart total drops the cents" 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/8c7fd612-4266-4724-a9c4-97000b3c9925">TryBugHunt</a></p>