Comparing Split Integer

Hey everyone I need a bit of help. I read and search online, and nothing really help me solve my problem for this program.

Program: Compares two positive integers entered at the keyboard and returns the number of
decimal places (i.e. the ones, tens, hundreds place, and so on) that have the same digit.

With the option 2, validate your input values: do not accept character(s) and negative and/ or
floating-point values.

This is what I have so far..... (ignore that I didn't put in #include because this is a menu-driven program and I have already solve the first part)

{
int number;
stringstream temp;


cout << endl;
center("Enter your positive integer : ");
cin >> number;
stringstream temp1;
temp1 << number;


center("Enter your positive integer : ");
cin >> number;
cout << endl;
stringstream temp2;
temp2 << number;


cout << "Number 1: " << temp1.str() << endl;
cout << "Number 2: " << temp2.str() << endl << endl;
cout << "Number of comparison: " << temp.str().size() << endl;


}


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
#include <iostream>
#include <sstream>

// returns the number of positions that have the same digit
// invariant: first > 0 && second > 0 (positive integers)
int do_count_same_digits( int first, int second )
{
    if( first==0 || second==0 ) return 0 ;
    const int a = first%10 ;
    const int b = second%10 ;
    return (a==b) + do_count_same_digits( first/10, second/10 ) ;
}

int read_positive_int()
{
    std::string input ;
    std::cout << "please enter a +ve integer: " ;
    std::getline( std::cin, input ) ; // read the input into a string

    {
        std::istringstream stm(input) ; // create an input string stream
        int v ;
        char c ;
        // if we have read 'v' and 'v' is +ve and we fail to read another
        // non whitespace character, str contains a +ve integer and nothing else
        if( stm >> v && v > 0 && !( stm >> c ) ) return v ;
    }

    std::cout << "error in input! " ;
    return read_positive_int() ; // try again
}
Can you explain to me further or more in details how you do this? I don't want to copy and take it as my work. Rather I want to learn from it and see what I need to do in order to learn so I can use it in the future.

Thanks JLBorges
Last edited on
> I don't want to copy and take it as my work.
> Rather I want to learn from it and see what I need to do in order to learn

Great. That was the assumption under which I posted the snippet.

Which is the part that you do not understand?
do_count_same_digits() or some part of read_positive_int()?
The do_count_same_digits(), can you explain further on that? I am new to c++ (forgot to mention it).

Since I am trying to compare let say

The first integer is : 33
Second integer is : 334

Number of comparison: 2

3 <---> 4
3<---->3 <correct>

Same number of digit:1

Would array work as well for this type?
Well the idea is this:

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
do_count_same_digits( a6a5a4a3a2a1a0, b2b1b0 )

first:  a6a5a4a3a2a1a0

second: b2b1b0

a0 == first % 10, and b0 = second % 10

a6a5a4a3a2a1 == first / 10, and b2b1 = second / 10

if( b0 == a0 )
    count_same_digits = 1 + do_count_same_digits( a6a5a4a3a2a1, b2b1 )
else      
    count_same_digits = 0 + do_count_same_digits( a6a5a4a3a2a1, b2b1 )
    
ie. ( b0 == a0 ) + do_count_same_digits( a6a5a4a3a2a1, b2b1 )   

or  ( b0 == a0 ) + do_count_same_digits( first/10, second/10 )

...............................................................

do_count_same_digits( a6a5a4a3a2a1, b2b1 )

first:  a6a5a4a3a2a1

second: b2b1

a1 == first % 10, and b1 = second % 10

a6a5a4a3a2 == first / 10, and b2 = second / 10

if( b1 == a1 )
    count_same_digits = 1 + do_count_same_digits( a6a5a4a3a2, b2 )
else      
    count_same_digits = 0 + do_count_same_digits( a6a5a4a3a2, b2 )
    
ie. ( b1 == a1 ) + do_count_same_digits( a6a5a4a3a2, b2 )

and so on till one of the numbers reaches zero

.....................................................

Which gives us:

do_count_same_digits( a6a5a4a3a2a1a0, b2b1b0 ) 
    == (a0 == b0 ) + ( a1 == b1 ) + do_count_same_digits( a6a5a4a3a2, b2 )   
    == (a0 == b0 ) + ( a1 == b1 ) + ( a2 == b2 ) + do_count_same_digits( a6a5a4a3, 0 )
    == (a0 == b0 ) + ( a1 == b1 ) + ( a2 == b2 ) + 0


The first integer is : 33
Second integer is : 334

do_count_same_digits( 33, 334 ) 
== ( 3 == 4 ) + do_count_same_digits( 3, 33 )
== ( 3 == 4 ) + ( 3 == 3 ) + do_count_same_digits( 0, 3 )
== ( 3 == 4 ) + ( 3 == 3 ) + 0
== 1



Would array work as well for this type?

Yes. Two arrays containing the separated digits of the two numbers, and then count how many elements in the two arrays at the same position are equal. Ignoring leading zeroes, of course.
Last edited on
Thank you I get the jist of it
Topic archived. No new replies allowed.