Help with C++ perfect number program

  • Comp Sci
  • Thread starter gbsboard
  • Start date
  • #1
4
0
Ok I've been having alot of trouble with this program, I am also very new to C++. The program requires to have 4 functions 1 main, 1 svr, 2 void functions
any kind of loop (for loop preferred) in the main function so that a user can enter any number of positive integers in the main function. One function is called sum_of_divisors which accepts an integer and returns the sum of its perfect divisors.

The program also prints out the input value as well as a message indicating whether the number is perfect or not

Example

Enter Number you want to test: 12
Sum of Divisor = 16
12 is not a perfect number


Enter Number you want to test: 6
Sum of Divisor = 6
6 is a perfect number

So far I have as my code

Code:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//File Name: Perfect Numbers
//Author: ********
//Email Address: ***********
//Homework Number: 3
//Description: Tests input number to see whether or not they
//are perfect numbers.
//Last Changed: feb 2, 2011

#include <iostream>
using namespace std;

//* * * * * * * * * * * * * * * * * * * * * * * * * *
//* Funtion: get_data
//* purpose: reads the value used 
//*
//* parameters: Num1 - the first number
//* calls:  None
//* * * * * * * * * * * * * * * * * * * * * * * * * * *

void get_data (int& num1);

//* * * * * * * * * * * * * * * * * * * * * * * * * *
//* Funtion: Sum of Divisors
//* purpose:  Figures out whether what the divisors are
//* parameters: Divisor - gets the number
//*             
//* calls:  None
//* * * * * * * * * * * * * * * * * * * * * * * * * * *



void sum_of_divisors(int num1);

//* * * * * * * * * * * * * * * * * * * * * * * * * *
//* Funtion: Results
//* purpose:  Shows the results of the function
//* parameters: Divisor - shows the perfect
//*             
//* calls:   get_data
//* * * * * * * * * * * * * * * * * * * * * * * * * * *

void show_results (int num1);

int num1;

int main()
{
	int num1;

	get_data(num1);
	while (num<=0);
	for(int divisor=1; divisor<=num1; divisor++)
	{
	show_results(num1)
	get_data(num1)
	}
	return(0);
}

void get_data(int& num1)
{
	using namespace std;
	cout <<"\n Enter number to test to see if it is a perfect number ";
  	cin >> num1;
}

void sum_of_divisors(int num1)
{
int sum=0;
   
    for(int divisor = 1; divisor <=(num1 / 2); divisor++)

    if(num1 % divisor == 0)
        sum += divisor;
return;

void show_results(int num1)
{
	sum_of_divisors();
	cout<<" \n\nThe perfect divisors for the input number \n\n"<<
	cout<<" Number being tested:   " << num1 <<" Divisors:   " << sum_of_divisors <<endl<<endl;	
}
 

Answers and Replies

  • #2
297
2
I'll just start out by pointing out that it's spelled "Function". :)

Here's some of the problems I see, without getting into the logic.
Code:
int num1;

int main()
{
	int num1;
You have a global 'num1' there, but also one in your main. You probably shouldn't have the global variable there.
Code:
	get_data(num1);
	while (num<=0);
That while loop will go on forever if num is <= 0, and will do absolutely nothing. However, you didn't declare a variable called 'num'. I'm guessing you meant for that get_data() to be inside the while loop, and to use num1.
Code:
	for(int divisor=1; divisor<=num1; divisor++)
	{
	show_results(num1)
	get_data(num1)
	}
	return(0);
}
You're missing semi-colons on both function calls in that for loop. Also, return isn't a function, so you don't need the parentheses. Doesn't hurt, but really serves no purpose here.
Code:
void get_data(int& num1)
{
	using namespace std;
	cout <<"\n Enter number to test to see if it is a perfect number ";
  	cin >> num1;
}
You don't need that using namespace std in there. It's already at the top of the program. Wouldn't think that would even compile, but maybe. Never tried putting it in the middle of a function.
Code:
void sum_of_divisors(int num1)
{
int sum=0;
   
    for(int divisor = 1; divisor <=(num1 / 2); divisor++)

    if(num1 % divisor == 0)
        sum += divisor;
return;
You're missing a curly brace after the return statement.
Code:
void show_results(int num1)
{
	sum_of_divisors();
	cout<<" \n\nThe perfect divisors for the input number \n\n"<<
	cout<<" Number being tested:   " << num1 <<" Divisors:   " << sum_of_divisors <<endl<<endl;	
}
Those two cout lines are pretty off. You either need to replace that last << on the first cout line with a semi-colon or make it into this (assuming I didn't make a syntax mistake or something):
Code:
	cout<<" \n\nThe perfect divisors for the input number \n\n"
	      <<" Number being tested:   " << num1 <<" Divisors:   "
              << sum_of_divisors <<endl<<endl;
Anyways, that's a quick pass through to find the errors that jump out at me. Hopefully enough to get you rolling.
 
  • #3
34,884
6,624
If you want help, help us out by telling us what problems you are having with your code.

What's an svr function?
 
  • #4
4
0
ok the problem i keep getting now is that it will prompt me to get a number so i'll type in 6 and then it stops and wont run any more of the program but will let me keep typing in numbers/letters and I am still figuring out where cout so that it will say "if the sum of the divisor = num1" such as 6 because 1+2+3=6 then cout<<"6 is a perfect number" but if the sum of the divisor doesnt = num1 then 6 is not a perfect number. I have updated the source code with the corrections suggested but i still am not sure what i am supposed to do with

get_data(num1);
while (num1>=1);

I know that the highest number I could need for the program would be 8128.




Updated code
Code:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//File Name: Perfect Numbers
//Author: xxxxxx
//Email Address: xxxxxxxx
//Homework Number: 3
//Description: Tests input number to see whether or not they
//are perfect numbers.
//Last Changed: Feb 2, 2011

#include <iostream>
using namespace std;

//* * * * * * * * * * * * * * * * * * * * * * * * * *
//* Funtion: get_data
//* purpose: reads the value used 
//*
//* parameters: Num1 - the first number
//* calls:  None
//* * * * * * * * * * * * * * * * * * * * * * * * * * *

void get_data (int& num1);

//* * * * * * * * * * * * * * * * * * * * * * * * * *
//* Funtion: Sum of Divisors
//* purpose:  Figures out whether what the divisors are
//* parameters: Divisor - gets the number
//*             
//* calls:  None
//* * * * * * * * * * * * * * * * * * * * * * * * * * *



void sum_of_divisors(int num1);

//* * * * * * * * * * * * * * * * * * * * * * * * * *
//* Funtion: Results
//* purpose:  Shows the results of the function
//* parameters: Divisor - shows the perfect
//*             
//* calls:   get_data
//* * * * * * * * * * * * * * * * * * * * * * * * * * *

void show_results (int num1);

int main()
{
	int num1;

	get_data(num1);
	while (num1>=1);
	for(int divisor=1; divisor<=num1; divisor++)
	{
	show_results(num1);
	get_data(num1);
	}

	return(0);
}

void get_data(int& num1)
{
	cout <<"\n Enter number to test to see if it is a perfect number ";
  	cin >> num1;
}

void sum_of_divisors(int num1)
{
int sum=0;
   
    for(int divisor = 1; divisor <=(num1 / 2); divisor++)

    if(num1 % divisor == 0)
        sum += divisor;
return;
}

void show_results(int num1)
{
		
	cout<<" \n\nThe perfect divisors for the input number \n\n"
	      <<" Number being tested:   " << num1 <<" Divisors:   "
              << sum_of_divisors <<endl<<endl;	
	

}




My professor never really told us what a SVR function was 90% of the class figured it ment the void sum_of_divisor




and thank you for all the help I have been trying to figure out this program for a little over 8 hours and it's just making me a lot less interested in computer programming than I was.
 
Last edited:
  • #5
34,884
6,624
That semicolon at the end of your while loop makes it an infinite loop.

while(num1 >= 1);

If you have entered a value for num1 that is 1 or larger, your loop runs forever, and never goes to the statements following the loop.

You need to think more about what your main function should do, and specifically, what should be going on in main versus what should be going on in show_results() and sum_of_divisors().

One possible organization would be this:
Code:
// in main()
int num = 1;

  while (num >= 1)
  {
     show_results(num1);
     get_data(num1);
  }
  return 0;
The code for show_results calls the sum_of_divisors function, but you are not calling this function correctly - you need to have sum_of_divisors(num1), which you don't.

Also, you really should change "Funtion" to "Function" as grep suggested.
 
  • #6
297
2
In addition to what Mark said, you need to return a value from this function:
Code:
int sum_of_divisors(int num1)
{
    int sum=0;
   
    for(int divisor = 1; divisor <=(num1 / 2); divisor++)
        if(num1 % divisor == 0)
            sum += divisor;

    return sum;
}
Right now, that sum is stuck in the function and ceases to exist once it exits. Also, note that I fixed the indentation. It makes it clear that the if statement is inside the for loop, which is very important. I would just use curly braces for clarity, myself, but that's up to you. At minimum, make sure to indent it correctly.

This also means you need to pass that sum value into show_results, which should have a signature such as:

void show_results (int num1, int sum);
 
Last edited:
  • #7
4
0
Alright i was able to get the program to function 100% perfectly need to thank you for all the help it is very much appreciated.
 
  • #8
129
0
Code:
int sum(int n){
        int s=1,d;
        for(d=2; d*d<n; d++)
                if (n%d==0)
                        s+= d + n/d;
        if (d*d==n)
                s+=d;
        return s;
}

but...
http://en.wikipedia.org/wiki/Perfect_number#Even_perfect_numbers"
Code:
#include <stdio.h>
unsigned long long int s,n1;
unsigned int p,i,n;
 
int main(){
        p =2; 
        n =3;  // n== 2^p - 1
        n1=2;  // n1*n perfect number if n is prime
        do{
                s=4;                     // 1  Lucas-Lehmer test
                for(i=3; i<=p; i++)      // 2
                        s=(s*s-2) % n;   // 3
                if (s==0 || p==2)        // 4
                        printf("Mersenne prime=2^%2d-1=%10d, perfect number %20llu\n", p, n, n1*n);
                p++;
                n1=n+1;
                n=2*n+1;
        }while (p<32);
        return(0);
}
Output
Code:
Mersenne prime=2^ 2-1=         3, perfect number                    6
Mersenne prime=2^ 3-1=         7, perfect number                   28
Mersenne prime=2^ 5-1=        31, perfect number                  496
Mersenne prime=2^ 7-1=       127, perfect number                 8128
Mersenne prime=2^13-1=      8191, perfect number             33550336
Mersenne prime=2^17-1=    131071, perfect number           8589869056
Mersenne prime=2^19-1=    524287, perfect number         137438691328
Mersenne prime=2^31-1=2147483647, perfect number  2305843008139952128
 
Last edited by a moderator:

Related Threads on Help with C++ perfect number program

Replies
7
Views
5K
  • Last Post
Replies
8
Views
5K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
5
Views
2K
Replies
1
Views
3K
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
2
Views
906
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
4
Views
2K
Replies
8
Views
7K
Top