An integer type has a finite range. Do not assume a particular number of bytes or that every platform has the same largest int. <limits> supplies std::numeric_limits<int>::max() to ask the implementation for its largest supported value.
Signed integer overflow is undefined behavior; you cannot rely on a wrapped result. Using an unsigned type would give defined wraparound, but that would still be the wrong result for a total that must fit. A guard must run before the addition; checking a result after overflow is too late. For nonnegative operands a and b, adding is safe when a <= maximum - b. Subtraction is safe here because b is between zero and the maximum.
#include <iostream>
#include <limits>
bool fits(int amount, int extra) {
if (amount < 0 || extra < 0) return false;
int maximum = std::numeric_limits<int>::max();
return amount <= maximum - extra;
}
int main() {
std::cout << std::boolalpha << fits(40, 2) << '\n';
}
This helper reports whether addition is allowed; it never performs the sum. Its contract intentionally covers only nonnegative values. A general signed-arithmetic helper would need additional reasoning for negative operands and lower bounds. Narrow, documented contracts are easier to check correctly.
In the editor, the supplied negative-value guard already establishes the subtraction's safe range. Replace the remaining placeholder condition. The probes ask about an ordinary pair and about the largest integer plus one, but they never calculate the unsafe sum. Then complete addChecked, which calls canAdd before changing the output argument. Finish task 1 first: while canAdd always rejects the request, the success path in task 2 cannot run. On rejection, leave result unchanged. That pattern connects range checking to a useful operation instead of merely printing a platform-specific limit.
Edit main.cpp; the supplied Console probes already call your code.
canAdd using the safe comparison against max() - b. Expect Small fits: true and Too large fits: false.addChecked, assign the sum to result and return true for this successful operation. Expect Added: true 20.Use Run to inspect the Console, then Submit to check your work.