Thread: C++ Beginner; need help with IsPerfect >.>

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    C++ Beginner; need help with IsPerfect >.>

    Yeah, I picked up an old C++ textbook about a week ago and it seems pretty interesting. Using A Computer Science Tapestry by Astrachan. Anyway, I need help with the boolean, IsPerfect. I've gotten this far, but I just can't seem to fix it up.
    Code:
    bool IsPerfect(int n)
    {	
    	int sum = 0;
    	int x;
    	int divisor;
    
    	x = n;
    
    	while(x > 0)
    	{
    		divisor = x;
    
    
    		if(n % divisor == 0)
    		{
    			sum += divisor;
    		}
    		x--;
    	}
    
    	if(sum == n)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    Note: I am basing this off the textbook's example of IfPrime, so any help would be appreciated. Also running a second cpp file for the purpose of testing this boolean. Thank you.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    x = n;

    your

    if(n % divisor == 0)
    will be true

    so you always start with the sum = n
    adding other divisors
    obviosly - you never get n as a result
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    Mmm

    Yeah, thanks. I actually got rid of X since it was actually pretty much just divisor (figured divisor served as a countdown or method to exit the loop as well). And instead of having X getting smaller towards 0, I changed it to the divisor getting bigger towards N or specified value. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  4. beginner help please
    By edusa in forum C Programming
    Replies: 11
    Last Post: 05-15-2002, 05:20 AM