Simple "sum of digits" code. Compiles but when executed, the last cout gives a "0" for the num int rather than the actual user-input number.
Feel free to c&p into your own compiler, if you're so inclined, to see what I mean.
How can I get this to output the correct "num" value?
Thank you!
~~~
#include <iostream>
using namespace std;
int main()
{
int num;
int sum = 0;
cout << "Please type any non-negative integer: ";
cin >> num;
while ( num > 0 ) {
sum += num % 10;
num /= 10;
}
cout << "The sum of the digits of " << num << " is " << sum << "\n";
system("PAUSE");
return 0;
}