Pulling individual numbers from an input

I'm having a problem with my program. When I input 1234, I get the correct display: 1 2 3 4 and the sum is 10.
However, when I input more than 4 numbers, the display is wrong and therefore it adds the wrong numbers. I can't figure out what's wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iomanip>
#include <iostream>
#include<cmath>

using namespace std;

int mainudf();

int main ()
{
    mainudf();
}

int mainudf()
{
    int number;
    int i = 0;
    int digit = 0;
    int sum = 0;

    cout << "This program will accept an integer and output the individual numbers of the integer. \n"
        << "It will then display the sum of the individual digits.\n"
        << "Please enter an integer" << endl;
    cin >> number;
    cout << endl;

    number = abs ( number );         // takes the abs value of the number so negative numbers can be used.

    int copynumber = number;              // storing the number into a diff variable

    // Find the number of digits
    do
    {
        i ++;

        copynumber = copynumber / 10;
    }
    while ( copynumber != 0 );
    //using the power function to remove digits from the number
    int divisor = pow ( 10 , i - 1 );

    // Take out each digit from the left, calculate sum and print the digit
    do
    {
        digit = number / divisor;                  // Take out a digit

        number = number - ( digit * divisor );    // Update integer

        cout << digit << ' ';

        sum = sum + digit;

        divisor /= 10;                              // Take a zero off the divisor
    }
    while ( divisor > 0 );

    cout << "\nSum = " << sum << endl;

    return 0;
}
Seems to work on the online cpp.sh environment.

What was your input+output that goes wrong?


Other notes:

Line 47 could use modulus operator (%).

Lines 36 and 51: there are operators /= and +=

One could generate the divisor in the first do..while loop, i.e. without pow().

Storing all digits in a stack container would avoid the need of divisor.

Using a recursive function would avoid the need of divisor too.
When I use the online cpp.sh enviroment, it still does not work for large numbers:
1
2
3
4
5
6
7
8
This program will accept an integer and output the individual numbers of the integer. 
It will then display the sum of the individual digits.
Please enter an integer
56164989879

2 1 4 7 4 8 3 6 4 7 
Sum = 46
 


When using codeblocks, it does not work for numbers over four digits:
1
2
3
4
5
6
7
This program will accept an integer and output the individual numbers of the integer. 
It will then display the sum of the individual digits.
Please enter an integer
12345

1235
Sum = 11
The two billion and change is the largest value that type int can hold (on cpp.sh's platform).

See http://www.cplusplus.com/reference/limits/numeric_limits/


What is the compiler (version) used by your codeblocks? What does std::numeric_limits<int>::max() return there?
My compiler is GNU GCC. The version of codeblocks is 13.12, I'm not seeing the version of the compiler though.

The limits of it is the 2bil number that showed up on cpp.sh
Also, for the do-while starting on 43, I'd like to use something like the following but I'm not sure how to go about implementing it.
1
2
3
4
5
6
7
8
    do
    {
     digit = number % 10;
     cout << digit << endl;
     sum = sum + digit;
     number = copynumber - digit;
    }
There is an entirely approach, which should be able to handle much larger input:

1. Read into std::string.
2. Take one character at a time from the string
2b. Convert character into int, show and add to sum
3. Show sum

You can also test that each character in string is a digit.


Edit: To see the version of the compiler, do run on command-line:
gcc --version

Last edited on
Topic archived. No new replies allowed.