0

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

Edited by pyTony: fixed formatting

2
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by ollie335
0

>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';
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.