Ok i apologize in advance for my ignorance but i have just started learning c++ and my first program went off without a hitch now this one that im doing completely on my own is giving me some hiccups. The best i can tell is that im just having some problems compiling it through xcode or its something that i am not experienced enough yet to diagnose any help would be greatly appreciated.

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
 const int LIMIT = 10000;
 int number = 2;
 while (number <= LIMIT) {
   int sumofDivisor = 0;
   int posDivisor = 1; 
   while (posDivisor <= number / 2)
     if (number % posDivisor == 0)
	   sumofDivisor = sumofDivisor + posDivisor;
	   if (number == sumofDivisor)
	     cout << number << "is perfect. \n";
	posDivisor ++;
	}
 number ++;
}

Dani AI

Generated

Quick diagnosis and what to check (short): the symptom of the program printing the same first value repeatedly almost always means a loop control statement (the counter increment or a reset) is not inside the loop body the way you expect. That happens when braces are missing or mis-placed so the compiler treats only the next single statement as the loop body. , and pointed toward the right areas—brace placement and program structure—but the useful fix is to verify which statements are actually grouped inside each loop and to trace the loop variables as the program runs.

Practical checklist to find and fix the bug:

  • Reformat and indent the file so nesting is obvious; use your editor's brace-matching feature.
  • Make sure the accumulator that sums divisors is reinitialized at the start of each outer iteration.
  • Confirm the inner-loop increment is executed inside the inner loop and the outer-loop increment is inside the outer loop. If either increment sits outside its intended block, the corresponding loop will not progress.
  • Test with a tiny upper bound so you can watch behavior. Add a couple of temporary debug prints (counter and accumulator) or step through with the debugger.
  • Turn on compiler warnings (for example, -Wall -Wextra) and fix any warnings; they often point to misplaced statements.
  • For clarity and fewer mistakes, consider using counted for-loops for the counters and, later, an algorithmic improvement (only test divisors up to sqrt(n)).

If you need to step through in Xcode, use LLDB breakpoints and watch variables to see exactly which statement is—or is not—executing. For background on how braces form compound statements and how while loops bind single statements, see the language pages and a quick debugger guide:

  • Compound statements and scoping:
  • while statement semantics: https://en.cppreference.com/w/cpp/language/while
  • LLDB quickstart (useful inside Xcode):

Fix the grouping/braces and trace the loop variables; the repeated “2” output will stop once the increments and resets are inside the correct blocks.

Recommended Answers

All 6 Replies

Well the first thing I see is you don't have your int main() function closed, try adding a } to the end of all that.

Cameron

Your program as posted compiles without error for me. using VC++ 2008 Express. Looks like a logic error though -- you need more brackets around that last while loop (lines 14-18).

The brackets are fine. Add "return 0;" at the end of the main.

ok so that fixed the problem of nothing being printed but i'm having trouble that it wont increase now it just says 2 is perfect then 2 is perfect

The brackets are fine. .

Apparently you didn't bother to read/understand the program. There are not enough brackets { and }.

First you have too many loops. You don't need that outer loop that starts on line 11. So delete lines 11 and 20.

line 14: replace number/2 with LIMIT.

put open bracket at line 15 and close at line 20.

Align the program indentions correctly so that you can see what's going on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.