stopping loop but contiune

so im writing a prgm that find the perfect number where the user enter from x to y. such perfect numbers are 6, 28, 496, and 8128.
my prgm:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;
int main()
{
int start,end;
cout<<"Starting number: ";
cin>>start;
cout<<"Ending number: ";
cin>>end;
for (int outer=start; outer<end; outer++) 
	{
	int sum=0;
	for (int inner=1; inner<=outer/2; inner++)
	if (outer%inner==0) 
		sum += inner;
	if (sum==outer) 
		cout<<outer<<" is a Perfect Number."<<endl;
	else 
	cout<<"No Perfect Numbers found between "<< start <<" and "<< end <<endl; 
	} 
return 0;
}

i tried using break and continue statement, but using them simply doesnt work as the compiler say illegal use without matching if .
i am trying to find a way so that when the prgm run, it will loop from x to y and see if there is any perfect number or not.
with perfect num:
6 is a Perfect Number.
28 is a Perfect Number.

w/o any perfect num:
No Perfect Numbers found between 29 and 66


is there any possible way to make line 18 and 19 to only appear when there is no perfect number found? without using the break statement of course. but that will end the prgm....im kinda lost
make another int abc = 0 at the begining of main.
delete line 18 and 19.
modify ur loop to such a way that when u find a perfect number that the int abc increments
after the loops put a if/else statement in which if int abc is 0, then u found no perfect numbers, else nothing appears.

hope it helps
a goto?
so this is what i tried
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using namespace std;
int main()
{
int start,end,abc=0;
cout<<"Starting number: ";
cin>>start;
cout<<"Ending number: ";
cin>>end;
for (int outer=start; outer<end; outer++) 
    {
    int sum=0;
    for (int inner=1; inner<=outer/2; inner++)
    if (outer%inner==0) 
        sum += inner;
    if (sum==outer)
        cout<<outer<<" is a Perfect Number."<<endl;
        abc+=1; //only add 1 if sum==outer
        {if (abc==0)
        cout<<"No Perfect Numbers found between "<< start <<" and "<< end <<endl; 
        }
    } 
system("Pause");
return 0;
}
}

then i remember that you said
after the loops put a if/else statement in which if int abc is 0, then u found no perfect numbers, else nothing appears.

so i moved line 19-21 to after line 22 and it still doesnt work. entered 2 number: 9 and 19 and it just ended there. shouldnt abc==0 and display no perfect number found when the prgm checked and saw that there is no number between 9 and 19 thats a perfect number?
You have put the "No perfect numbers found" part in the wrong place.

So to put you out of your misery:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
using namespace std;
int main()
{
    int start,end,abc=0;
    cout<<"Starting number: ";
    cin>>start;
    cout<<"Ending number: ";
    cin>>end;
    for (int outer=start; outer<end; outer++) 
    {
        int sum=0;
        for (int inner=1; inner<=outer/2; inner++)
        if (outer%inner==0) 
            sum += inner;
        
        //Check if current number is a perfect number
        if (sum==outer)
            {
                //if so -  inform the user
                cout<<outer<<" is a Perfect Number."<<endl;       
                //update the count of prefect numbers found so far.
                abc+=1; //only add 1 if perfect number
            }

    } 
    
    //OK all numbers have been checked 
   //if the count of prefect numbers found is still 0 at this point
   if (abc==0)
   {
       //Then output an appropriate message
       cout<<"No Perfect Numbers found between "<< start <<" and "<< end <<endl; 
   }   
   
    system("Pause");
    return 0;
}
the only thing i see why my prgm didnt work, even after i moved
1
2
3
{if (abc==0)
        cout<<"No Perfect Numbers found between "<< start <<" and "<< end <<endl; 
        }
was a simple {} for
1
2
3
 if (sum==outer)
      { cout<<outer<<" is a Perfect Number."<<endl;
        abc+=1;} //only add 1 if sum==outer 

can someone clarify to me why did a simple {} somehow make the prgm display the correct output? from what i understand,
1
2
3
 if (sum==outer)
        cout<<outer<<" is a Perfect Number."<<endl;
        abc+=1; //only add 1 if sum==outer 
should have been read and compile since it belonged to that if statement, and it also ended within the main()
But for some reason, by adding that {}, it just solved the problem
dznguy wrote:
if (sum==outer)
cout<<outer<<" is a Perfect Number."<<endl;
abc+=1; //only add 1 if sum==outer

should have been read and compile since it belonged to that if statement, and it also ended within the main()
But for some reason, by adding that {}, it just solved the problem


You need to re-read your documentation on how the if stement block is structured.
Or you can read it here:
http://www.cplusplus.com/doc/tutorial/control/

You will find that the way you have written it - cout<<outer<<" is a Perfect Number."<<endl; is part of the if
statement - but abc+=1; //only add 1 if sum==outer is not.
Last edited on
Topic archived. No new replies allowed.