Why doesn't this loop work?

A

Archie

Hi, I'm a beginner to c++. I wrote a program that identifies perfect
numbers (up to about 8 digits). You enter a number and the program
determines whether it is a perfect number.

Definition of a perfect number: a number whose factors (excluding the
number itself) sum to equal the number.

The first four perfect numbers are 6, 28, 496, 8128.

So the program I wrote to identify perfect numbers works. Here it is,
excluding the beginning lines through int main() and the last line:

int x;


cout << "Enter a positive integer.\n";
cin >> x;

int sum = 0;

for(int i = 1; i <= x/2; ++i){
if(x % i == 0)
{
sum += i;
}
}


if(sum == x){
cout << x << " is a perfect number.\n";
} else {

cout << x << " is not a perfect number.\n";
}


But when I write a loop to get the program to identify the first n
perfect numbers, the program fails no matter how few numbers I ask
for. Here it is:

int x = 1;
int n;

cout << "Enter a positive integer.\n";
cin >> n;

int sum = 0;

while(n > 0){


for(int i = 1; i <= x/2; ++i){

if(x % i == 0)
{
sum += i;
}

}


if(sum == x){
cout << x ;
n--;
}

x++;
}

Apparently, this gives an infinite loop? Nothing happens on the screen
after you enter the integer n.

What's the problem?

Thanks in advance.

Archie
 
A

Archie

You have some really strange indenting rules. I suggest you clear it
up so you can see where your errors are or at least be able to match
braces.






You should include all your code, including main() so others can
compile it with less effort and duplicate your problem.












You changed n for x, why?




// use do-while here, not while.>while(n > 0){

Your first tests used x, now you input n and compute on x, your input
value is always 1 because x = 1; above.





             ^    // Why?>            }


         ^----- Why?




Correct and tested program below:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main(void)
{
    int x;

    do
    {
        cout << "Enter a positive integer." << endl;
        cin >> x;

        int sum = 0;

        for(int i = 1; i <= x/2; ++i)
        {
            if(x % i == 0)
            {
                sum += i;
            }
        }
        if(sum == x)
        {
            cout << x << " is a perfect number." << endl;
        }
        else
        {
            cout << x << " is not a perfect number." << endl;
        }
    } while (x > 0);    

    return 0;



}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Thank you for your comments, but the corrected version does the same
thing as the first program I included (except that it repeats the
prompt to enter a positive integer). Here is the basic program, with
all the code:

#include <iostream>
using namespace std;

int main() {

int x;


cout << "Enter a positive integer.\n";
cin >> x;

int sum = 0;

for(int i = 1; i <= x/2; ++i){
if(x % i == 0)
{
sum += i;
}
}


if(sum == x){
cout << x << " is a perfect number.\n";
} else {

cout << x << " is not a perfect number.\n";
}

return 0;
}

My problem is not to create a program that identifies a perfect
number. THAT works for me. What I want to do is enter an integer n,
say 3, and have the program return the first 3 perfect numbers, which
would be 6, 28, 496.

I changed the x to an n to have the program loop through all numbers
x, starting from 1, until n perfect numbers were found. There's where
the loop doesn't work.

Here's the whole program (the one that does NOT work):



#include <iostream>
using namespace std;

int main() {

int x = 1; //x is the number to test; the program should begin with 1
and test each number until a perfect number is found.
int n; // n is the number of perfect numbers to identify.

cout << "Enter a positive integer.\n";
cin >> n;

int sum = 0;

while(n > 0){ //the while loop is intended to keep the loop going
while n > 0.

for(int i = 1; i <= x/2; ++i){ // the for loop should test
whether x is a perfect number. It's identical to the
//loop in the program above, where
the numbers 1 and 2 give the response that they are not perfect.
if(x % i == 0)
{
sum += i;
}

}


if(sum == x){ // if the sum = x, this program
should print x; then n decrements by 1; so the program
cout << x ; // should go on to find the next
perfect number.
n--;
}

x++; // here x is incremented, so the program
should test the next x, since this is contained in
} // the first while loop.

// But alas, such was not to be in
reality. What did I screw up?
return 0;
}


So this is the whole program. I've added comments.
 
A

Archie

#include <iostream>

using std::endl;
using std::cout;
using std::cin;

int main(void)
{
    int x = 1;
    int n;

    cout << "Enter a positive integer.\n";
    cin >> n;

    cout << "The first "<< n <<" perfect numbers are:" << endl;

    while (n > 0)
    {
        int sum = 0;

        for(int i = 1; i <= x/2; ++i)
        {
            if(x % i == 0)
                sum += i;
        }
        if(sum == x)
        {
            cout << x << endl;
            n--;
        }
        x++;
    }
    return 0;



}- Hide quoted text -

- Show quoted text -

That works. Thanks! I'm now trying to figure out why my original one
didn't work.

Archie
 
I

Ian Collins

My problem is not to create a program that identifies a perfect
number. THAT works for me. What I want to do is enter an integer n,
say 3, and have the program return the first 3 perfect numbers, which
would be 6, 28, 496.

I changed the x to an n to have the program loop through all numbers
x, starting from 1, until n perfect numbers were found. There's where
the loop doesn't work.

Here's the whole program (the one that does NOT work):

Your mixed up indentation style doesn't help!
#include<iostream>
using namespace std;

int main() {

int x = 1; //x is the number to test; the program should begin with 1
and test each number until a perfect number is found.
int n; // n is the number of perfect numbers to identify.

If variables require comments to explain what they are for, give them a
sensible name instead.
cout<< "Enter a positive integer.\n";
cin>> n;

int sum = 0;

while(n> 0){ //the while loop is intended to keep the loop going
while n> 0.

Nor do comments like that!

sum should be declared and initialised here.
 
A

Archie

Your mixed up indentation style doesn't help!




If variables require comments to explain what they are for, give them a
sensible name instead.




Nor do comments like that!

sum should be declared and initialised here.

Thanks for your comments. So if I initialize the sum before the while
loop, that's a logic error? I thought I could initialize variables at
the very beginning of the program.
 
F

Fred Zwarts

Archie said:
Hi, I'm a beginner to c++. I wrote a program that identifies perfect
numbers (up to about 8 digits). You enter a number and the program
determines whether it is a perfect number.

Definition of a perfect number: a number whose factors (excluding the
number itself) sum to equal the number.

The first four perfect numbers are 6, 28, 496, 8128.

So the program I wrote to identify perfect numbers works. Here it is,
excluding the beginning lines through int main() and the last line:

int x;


cout << "Enter a positive integer.\n";
cin >> x;

int sum = 0;

for(int i = 1; i <= x/2; ++i){
if(x % i == 0)
{
sum += i;
}
}


if(sum == x){
cout << x << " is a perfect number.\n";
} else {

cout << x << " is not a perfect number.\n";
}


But when I write a loop to get the program to identify the first n
perfect numbers, the program fails no matter how few numbers I ask
for. Here it is:

int x = 1;
int n;

cout << "Enter a positive integer.\n";
cin >> n;

int sum = 0;

while(n > 0){


for(int i = 1; i <= x/2; ++i){

if(x % i == 0)
{
sum += i;
}

}


if(sum == x){
cout << x ;
n--;
}

x++;
}

Apparently, this gives an infinite loop? Nothing happens on the screen
after you enter the integer n.

What's the problem?

Thanks in advance.

Archie

You never reset sum.
So, it will keep increasing and you will not find a perfect number.
So, the loop will not end.
 
J

Jonathan Lee

int x = 1;
int n;

cout << "Enter a positive integer.\n";
cin >> n;

int sum = 0;

while(n > 0){

        for(int i = 1; i <= x/2; ++i){

               if(x % i == 0)
                   {
                    sum += i;
                }

                }

        if(sum == x){
            cout << x ;
            n--;
            }

        x++;
        }

To be a little more explicit than the other answers:
- When first entering the for loop, x == 1. Thus,
x/2 == 0 and the for condition fails. The body
of the for loop is not executed, and consequently
"sum" does not change
- But then sum will not ever equal x in the if
statement (OK.. *eventually* it will, but it'll
take a while). So n never changes.
- Which is why your while loop is not exiting

--Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top