-1

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;
}

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

Swordfish
  • 12,971
  • 3
  • 21
  • 43
2 False
  • 1
  • 4

3 Answers3

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

Of course, this might violate some unspoken requirement.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • By far the best way given that the OP reads from standard input. I couldn't resist one edit. Do roll back if you don't like it. – Bathsheba May 07 '19 at 13:42
1

^ is not for exponentation, it's the XOR operator. Plus your algorithm is unnecessarily eloborate, and possibly defective.

Why don't you do something of the form, given the input non-negative number num,

for (sum = 0; num/*stop once all done*/; num /= 10/*remove the last digit*/){
    sum += num % 10; // Add the last digit to the sum.
}

?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thank you! I thought it recognized “^”. I implemented and substituted it with “pow” and it works! – 2 False May 07 '19 at 15:59
1
#include <limits>    // std::numeric_limits<>
#include <iostream>  // std::cin, std::cout, std::cerr

unsigned digit_sum(int number)
{
    if (number < 0)    // check for negative input
        number *= -1;  // and make it positive

    unsigned sum{};    // here we store our result
    while (number) {
        sum += number % 10;  // gives the last digit and adds it to sum
        number /= 10;        // removes the last digit
    }

    return sum;  // return the result
}

int main()
{
    int input;

    // Output the prompt and try to extract an int from std::cin:       
    while (std::cout << "Input your number: ", !(std::cin >> input)) {
        std::cerr << "Input error :(\n\n";  // inform the user
        std::cin.clear();                   // reset the stream
        // ignore all garbage that might be left until a newline is found:
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    // print the result:
    std::cout << "\nDigit sum: " << digit_sum(input) << "\n\n";
}

Notice that you'll have to remove the sign of a negative number for this to work correctly.

Swordfish
  • 12,971
  • 3
  • 21
  • 43