1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Problem with Int Function output

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

  1. Towely

    Towely New Member

    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

    EDIT: The program worked fine all along. I was just using incorrect input! Sorry, you can delete this thread.
     

Share This Page