There is actually a very simple solution to this problem. First, you are trying to do two things here:
1. Split an input number into separate digits.
2. Add the digits together.
First of all, I would not recommend putting your input into an int type. If you do this, you have to split each digit using modular arithmetic. While this is mathematically elegant and very pleasing aesthetically, it is not the optimal solution here. Instead, one could store the input as a string. In this case, each character can then be interpreted as a digit in and of itself and processed accordingly. Then, in one pass, one could output each digit and accumulate a running sum. The method is summarized here in pseudocode:
set integer sum = 0
get input -> store in string s
for each character c in s:
output c
sum = sum + integer( c )
output sum
The tricky part here is converting each character to an integer. Fortunately, this is very simple in c++: int i = c - '0'
This works because numbers in the ascii table are sorted in ascending order. Furthermore, chars are actually integers, so we can perform arithmetic on them. So, any numerical character minus the zero character results in the integer that the character represents: 7 == '7' - '0'
Using this method, the algorithm you are trying to build can be defined in a concise main function taking only 13 lines of code ( …