Hi, I've been trying to create a C++ program that gets the input from the user, calculates the amount of digits in the array and then calculates the sum of the digits

Heres what i've got so far

int main()
{
    char num[100];


    cout << "\nEnter Number: ";
    cin >> num;

    int counter = 0;
    for (int i = 0; num[i] != '\0'; i++)
        counter += 1;


    cout << counter << endl;

    int sum = 0;
    for (int a = 0; num[a] != '\0'; a++)
    {     
        num[a] = num[a] = '0';
        sum += num[a];
    }

    cout << sum;

    return 0;
}

but i cant seem to find the sum of the digits

Recommended Answers

All 2 Replies

>num[a] = num[a] = '0';
= is not the same as -. One is assignment, the other is subtraction. This is the line you want that will give you the correct sum:

num[a] = num[a] [B][I]-[/I][/B] '0';

Wow, thanks heaps Narue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.