1. Quick Diagnostic Table: Why Is My C++ Code Failing?
Before rewriting your whole program, match your symptom against this troubleshooting table:
| # | Buggy Code | What Goes Wrong | Correct Code |
|---|---|---|---|
| 1 | if (a = 5) | Assigns 5 to a instead of checking equality! | if (a == 5) |
| 2 | if (x > 0); { cout << x; } | Stray semicolon ends the if; cout always runs! | if (x > 0) { cout << x; } |
| 3 | while (n > 0) { s += n % 10; } | Infinite loop: program freezes because n never shrinks. | Add n = n / 10; inside the loop |
| 4 | int s; (without = 0) | Sum starts with random garbage value from RAM. | int s = 0, p = 1; |
| 5 | float m = (a + b) / 2; | Integer division: (5+6)/2 evaluates to 5, not 5.5! | float m = (a + b) / 2.0; |
| 6 | if (1 <= x <= 10) | Evaluates (1<=x) as 0 or 1, which is always <= 10! | if (x >= 1 && x <= 10) |
| 7 | while (n > 0) for digit count | Reports 0 digits for n = 0 instead of 1 digit! | Handle if (n == 0) or use do...while |
| 8 | Comparing ogl == n directly | After the loop, n is 0, so palindrome check fails! | Save a copy first: int aux = n; |
| 9 | Large products in int | Integer overflow past ~2.1 billion turns negative. | Use long long for products/factorials |
| 10 | Missing ; on previous line | Compiler highlights the NEXT line in red. | Check the line ABOVE the highlighted error |
2. Branching Traps: = vs ==, Range Checks, and Semicolons
Using = (assignment) instead of == (comparison) compiles without crashing, silently overwrites your variable, and breaks your logic. Likewise, chained math inequalities like 10 <= x <= 99 do not work as expected in C++; always combine two checks with logical AND (&&).
#include <iostream>
using namespace std;
int main() {
int x;
cin >> x;
// WRONG: if (10 <= x <= 99);
// RIGHT: explicit && operator and no stray semicolon
if (x >= 10 && x <= 99) {
cout << "Two-digit number";
} else if (x == 0) { // RIGHT: double equals
cout << "Zero";
} else {
cout << "Other number";
}
return 0;
}
3. Digit Algorithms (% 10, / 10, Palindromes & the n = 0 Edge Case)
Almost every middle school algorithm test includes digit extraction using modulo (% 10 for the last digit) and integer division (/ 10 to drop the last digit). Always back up n into a temporary variable (int aux = n;) before destroying n in the loop, and explicitly cover n = 0.
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int aux = n; // 1. Save original value BEFORE the loop
int rev = 0, sum = 0, digits = 0;
if (n == 0) { // 2. Handle n = 0 explicitly (1 digit)
digits = 1;
}
while (n > 0) {
int d = n % 10; // Extract last digit
sum = sum + d; // Accumulate sum
rev = rev * 10 + d; // Build reversed number
digits++; // Count digit
n = n / 10; // Chop last digit (REQUIRED!)
}
cout << "Digit sum = " << sum << "\n";
cout << "Digit count = " << digits << "\n";
if (aux == rev)
cout << "Palindrome!";
else
cout << "Not a palindrome!";
return 0;
}
If your console opens and prints nothing, check whether you forgot n = n / 10; inside your while loop or if the program is waiting for input at cin >> n.
4. Integer vs. Floating-Point Division and the 3-Glass Swap Rule
Dividing two int values like 7 / 2 yields 3, discarding the remainder. Divide by 2.0 or cast (float)a / b whenever you need decimals. And when swapping two variables a and b, always use a third temporary variable (aux = a; a = b; b = aux;) so neither value gets overwritten.
- Pour glass A into empty temporary glass AUX (aux = a;).
- Pour glass B into now-free glass A (a = b;).
- Pour the saved value from AUX into glass B (b = aux;).