Long Integer Problem: Output Errors

Write a function that takes as a parameter an integer (as a long value) and returns the number of odd, even, and zero digits.

Here is my code. I get bad output when I put the number: 23040598763
as input.

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
  #include <iostream>
using namespace std;

void digitCount(long num);

int main ()
{
    long num;
    
    cout <<"Enter a long number. " <<endl;
    cin >>num;
    cout <<endl;
    
    digitCount(num);
    
    system("PAUSE");
    return 0;
}

void digitCount(long num)
{
    int e = 0, z = 0, o = 0, x = 0;
    
    for (int i = 0; i <= 11; i++)
    {
        x = num % 10;
        
        if (x % 10 == 0)
           z++;
        
        if (x % 10 != 0)
        {
              if ((x % 2) == 0)
                 e++;
              else if ((x % 2) != 0)
                 o++;
        }
            
        num = num / 10;
    }
    
    cout << "number of zeros = " << z << endl;
    cout << "number of odd numbers = " << o << endl;
    cout << "number of even numbers = " << e << endl;
}


My output is:


number of zeros = 2
number of odd numbers = 6
number of even numbers = 4


The odd numbers should be 5 given the number I put in, and that same output comes up no matter what long number I put in. I need this soon...
Last edited on
A long number like 23040598763 does not fit into a 32 bit integer. Try using long long as the variable type or testing with numbers smaller than 2^32=4294967296
Last edited on
Heh heh, yeah, that would have been smart, but there's just one problem... I'm not allowed to use long long for this program.
One more thing I should mention: You are working with signed integers so really you should be using numbers that are less than 2^31. Since one bit is dedicated to the sign of the number.
Last edited on
But I have to use an 11 digit number!
Does the negative sign on a 10 digit negative number count as 11 digits? If not, I am afraid you are out of luck getting an 11 digit number from a 32 bit integer.
Darn... well thanks anyway!
I'm not allowed to use long long for this program.
But I have to use an 11 digit number!

I've seen this pattern before. Every suggested solution is shot down as being forbidden. It might be better to post the full question, including any restrictions, at the start.

But having said that a couple of other thoughts come to mind. One is the use of a string, the other is the use of the double type.
Last edited on
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
#include<iostream>
#include<string>

using namespace std;

void digitCount(string num);

int main()
{
    string num;//="23040598763";
    cout<<"Enter a long number. "<<endl;
    cin>>num;

    cout<<"\nThe number is: "<<num<<endl;
    cout<<"It has "<<num.length()<<" digits."<<endl;
    digitCount(num);
    cout<<"\nPress Enter to quit!";
    cin.get();
    cin.get();
    return 0;
}
void digitCount(string num)
{
    int e = 0, z = 0, o = 0, x = 0,c=0;

    for(size_t i = 0; i < num.length(); i++)
    {
        c = num[i]-48;
        x=c % 2;
        if(c!=0)
            {
                if(x==1)
                {
                    cout<<num[i]<<" is odd."<<endl;
                    o++;
                }
                if(x==0)
                {
                    cout << num[i] <<" is even."<<  endl;
                    e++;
                }
            }
        else if(c==0)
        {
            cout<<num[i]<<" is zero."<<endl;
            z++;
        }
    }
    cout << "number of zeros = " << z << endl;
    cout << "number of odd numbers = " << o << endl;
    cout << "number of even numbers = " << e << endl;
}
Topic archived. No new replies allowed.