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

This is a discussion on C++ Beginner; need help with IsPerfect >.> within the C++ Programming forums, part of the General Programming Boards category; Yeah, I picked up an old C++ textbook about a week ago and it seems pretty interesting. Using A Computer ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 10:21 AM
  4. beginner help please
    By edusa in forum C Programming
    Replies: 11
    Last Post: 05-15-2002, 05:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21