Help with finding sum of digits

I am currently trying to write a program that causes Additive Persistance to happen based on the numbers give in an input.txt. I was given the correct outputs beforehand, but I am not getting them in the last 3 inputs. Below is my code and also the outputs that are suppose to happen. Any insight would be greatly appreciated, thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>

int AdditiveP(int number) {
	
    int sum = 0;
    int i;
    
    for (i = number; i > 0; i /= 10)
        sum += i % 10;
	
	return number == sum ? 0 : (1 + AdditiveP(sum));
}

int main() {
	
	std::ifstream infile("input.txt");
	
	int number;
	int sum = 0;
	
	while(infile >> number) {
	
	int numsum = number;
        sum =+ (numsum % 10);
        numsum = numsum/10;
    	 
		if (number >= 0) {
			
		   std::cout << AdditiveP(number) << " " << sum << std::endl;
		}
		
		else {
			
			break;
		}
	}
}


The outputs I should be getting are:
0 3
0 8
0 9
2 1
3 1
2 9

The outputs I get are:
0 3
0 8
0 9
2 9
3 9
2 0
closed account (48T7M4Gy)
Check line 25 '+=' vs '=+'
After changing to +=, I got the following:
0 3
0 11
0 20
2 29
3 38
2 38
Please share the contents of your input file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
using namespace std;

int AdditiveP( int &number )
{
   int sum = 0;
   for ( int i = number; i > 0; i /= 10 )  sum += i % 10;
        
   if ( number == sum )
   {
      return 0;
   }
   else
   {
      int value = 1 + AdditiveP( sum );
      number = sum;
      return value;
   }
}


int main()
{
     int number;
//   std::ifstream infile("input.txt");
//   while ( infile >> number )
     while ( true )
     {
        cout << "Enter a number: ";   cin >> number;
        if ( number >= 0 ) 
        {
           int P = AdditiveP( number );
           cout << P << " " << number << endl;
        }
     }
}


Enter a number: 8
0 8
Enter a number: 123
1 6
Enter a number: 12345
2 6
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

int sum_of_digits(int aNumber){
    int sum = 0;
    
    while ( aNumber > 0 ){
        sum += aNumber % 10;
        aNumber /= 10;
    }
    
    return sum;
}

int digital_root(int aNumber){
    while( aNumber > 9 )
        aNumber = sum_of_digits(aNumber);
    return aNumber;
}

int additive_persistence( int aNumber ){
    int persistence = 0 ;
    
    while( aNumber > 9 ){
        persistence++;
        aNumber = sum_of_digits(aNumber) ;
    }
    
    return persistence;
}

int main(){
    for(int number = 0; number < 100; number++)
        std::cout << number << ' ' << additive_persistence(number) << ' ' << digital_root(number) << '\n';
    
    return 0;
}
Topic archived. No new replies allowed.