Outputting the sum of the digits of an integer

The program I am working on is supposed to receive an integer as input and output the digits separately and the sum of the digits. I can ouput each digit separately but am struggling with outputting the sum of the digits. Here is my code thus far.
char digit;
int sum=0;

cout << "Enter an integer ending with a: ";
cin.get(digit);
cout << endl;
while(cin && digit != 'a')
{
cout << digit << " ";
sum = sum + digit;
cin.get(digit);
}
cout << endl;
cout << sum;
sum = sum + digit;
This code is adding the int sum to the char digit.
The problem is
'1' != 1
'1' == 49
So In fact you are adding the ascii value for the digit to sum, instead of what you expect.
You need to find a way to convert a digit of type char into an int the way you want, so that
yourFunc('1') == 1
Topic archived. No new replies allowed.