I want to make a code that separates the values of individual digits in a number in c++. Ex: 12345 = 1+2+3+4+5

-1 c++

I cannot find why it isn't working. It outputs the number I inputted but with 1 digit less. Ex: 12345 ---> 1234.

I have already tried changing the while loop for a for one adding ifs and removing the parentheses.

#include <iostream>

using namespace std;

int num0, num1, x, y, z, num2;

int main()
{   
    cout << "input your number \n";
    cin >> num0;
    y = 0;
    x = 1;
    z = -1;
    num2 = 0;

    while (num0 > y)
    {
        y = (y * 10) + 9;
        z++;
    }

    while (z >= 0)
    {
        num1 = num0 / (10 ^ z);
        num0 = num0 - (num1 * 10 ^ z);
        z--;
        num2 += num1;
    }

    cout << num2;
}
Run Code Online (Sandbox Code Playgroud)

I want to input any number and then add the individual digits. Ex: 56868947 = 5+6+8+6+8+9+4+7 = 53

Pet*_*ker 5

If you rethink the problem it becomes much simpler. Instead of converting input text to a number and then converting the number to digits, just convert each character in the text directly:

std::string number;
std::cin >> number;
int sum = 0;
for (int i = 0; i < number.size(); ++i)
    sum += number[i] - '0'; // works for all encodings
Run Code Online (Sandbox Code Playgroud)

Of course, this might violate some unspoken requirement.