Problem with Int Function output

Discussion in 'C++' started by Towely, Nov 4, 2009.

  1. Towely

    Towely New Member

    Joined:
    Oct 22, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I'm having a problem with the output of my program.

    The "SumFunction" function adds the digits of a number together. For instance, the number 4532 would be 4 + 5 + 3 +2 and the output would equal 14.
    the rest of the program determines if the last digit of the "SumFunction" of 2 user-given numbers are the same. If so, they are "Sum Buddies". The problem is that the code always says that any two numbers are Not Sum Buddies, even if they actually are.

    It's a fairly simple code... Where am I going wrong?

    Code:
    #include <iostream>
    using namespace std;
    
    int SumFunction (int x)
    {
       int result = 0;
       result = ((x % 10) + ((x / 10) % 10) + ((x / 100) % 10) + ((x / 1000) % 10) + ((x / 10000) % 10));
       return (result);
    
    }
    
    int main ()
    {
       int a, b;
       cout << "Type Two Numbers: ";
       cin >> a >> b;
    
       if ((SumFunction(a) % 10) == (SumFunction(b) % 10))
       {
          cout << a << " and " << b << " are sum pals." << endl;
       }
       else
          cout << a << " and " << b << " aren't sum pals." << endl;
    
     
  2. Towely

    Towely New Member

    Joined:
    Oct 22, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    EDIT: The program worked fine all along. I was just using incorrect input! Sorry, you can delete this thread.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice