program not working as it should

The text of the problem sound like this(rough translation): You introduce from keyboard natural numbers until you enter 2 times consecutively the same number.
Write a program that will display from the numbers entered those who are in the form abac (has 4 digits, 1st digit=3rd digit) and the sum of its digits is a number with 2 identical digits (a+b+a+c=xx).

The result of my algorithm is that even when I enter numbers with the described property nothing gets displayed. Only the end-program condition works. (stops and returns 0). Please help.

Here is what I've done:

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


long form_abac(long a)            // this subprogram tests in the first conditional if the number has 4 digits
{
                                  // in the second conditional it tests to see if the first digit is equal to the third digit
    if(a/1000!=0 && a/10000==0)
    if(a/1000==(a/10)%10)
    return 1;

}



main ()
{
    long number1, number2, digit1, digit2, sum_digit1, sum_digit2, n1, n2; //number1 and 2 will be writen by the user
    number1=0; number2=1;     //we give at the beggining different values to number1 and 2 so it can pass the first while condition
    sum_digit1=0; sum_digit2=0;  //the digit sums are initialized with 0

    while(number1!=number2)     //the program must stop when we introduce the same numbers consecutively
    {


    cout<<"introduce the first number: ";
    cin>>number1;
    cout<<"introduce the second number: ";
    cin>>number2;
    n1=number1; n2=number2;    //we use n1 and n2 will be number1 and number2 that will be processed to see if they match the conditions

    while(n1!=0)     //through the next 2 while structures we find out the sum of the digits of the previously entered numbers
    {
    digit1=n1%10;
    n1/=10;
    sum_digit1+=digit1;
    }

    while(n2!=0)
    {
        digit2=n2%10;
        n2/=10;
        sum_digit2+=digit2;
                        //in the next if conditionals we test each sum of digits to see if they match the format aa, 2 equal digits
    }                   // and to see if the entered numbers match the conditions, using the subprogram form_abac

    if(sum_digit1%10==((sum_digit1%10)/10)%10 && form_abac(number1)==1)
    if(((sum_digit1/10))/10==0)
        cout<<"\n "<<number1<<endl;

        if(sum_digit2%10==((sum_digit2%10)/10)%10 && form_abac(number2)==1)
    if(((sum_digit2/10))/10==0)
        cout<<"\n "<<number2<<endl;


    }
}
i understand it like this:

example:
entered number: 1217
sum = 11
form = abac
this data gets displayed.

entered number: 3217
sum = 13
form = abcd
this data gets not displayed.

correct me when i'm wrong.

I still don't understand why you need to do it twice though?
Why not get a number once and save that value for comparison the next time, when the new number is the same as the previous, exit.

You use alot of math where it isn't needed, it is very hard code to read.

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
unsigned char digits[4];

long a = 2324;//entered number

digits[0] = (a/ 1000) % 10;
digits[1] = (a/ 100) % 10;
digits[2] = (a/ 10 ) % 10;
digits[3] = (a ) % 10;

if( ( digits[0] == digits[2] ) && // aXaX
    ( digits[1] != digits[3] )  && // XaXb
    ( digits[0] != digits[1] )  && // abXX
    ( digits[0] != digits[3] )   )  //aXXb
{
    //form = abac
}

sum = // you'l figure it out
...

Yes, you understood the problem. I used all that math because I tried to solve the problem exactly how my manual wants it solved, with all those details like for example in this one problem:
the text says you must enter numbers until you enter same thing twice in a row (and those numbers are not necessarily of 4 digits like in your example, you must be able to enter any number, but only those which respect the conditions must be displayed. Your version does the job but in a more particular manner. I would like to know what I've done wrong in my version or maybe someone could post another version of this algorithm that does the same job.

Thank you for your feedback.
ok, first this function:

1
2
3
4
5
6
7
8
long form_abac(long a)            // this subprogram tests in the first conditional if the number has 4 digits
{
                                  // in the second conditional it tests to see if the first digit is equal to the third digit
    if(a/1000!=0 && a/10000==0)
    if(a/1000==(a/10)%10)
    return 1;

}


what is being returned when a only has 3 digits? nothing at all, your compiler should give a message about this.

you do this to get the second digit of the sum:

((sum_digit1%10)/10)%10

but sum_digit1%10 results in the first character, not the second. And you divide this by 10 so this will always be 0.

you should use this:
((sum_digit1/10)%10)

next, do you mean like a fifo? you can create a 8 digit wide fifo and add new numbers at the beginning and delete the oldest numbers at the end. (maybe use it as a ringbuffer). You can compare the first 4 digits with the last four digits to determine whether they are the same.

and just use the latest 4 digits to check for the abac form and sum. this way only numbers 0-9 can be entered.
Sorry, I don't know how to use a fifo. I made a few corrections (including the ((sum_digit1/10)%10)). Now its seem to work for some numbers and don't for others. It's a very confusing one. I don't know, I'll play with it a bit more to see if I can fix it.
fifo: first in first out.

example:

0) hello
1) qwerty
2) some other data


now you enter a new element at 0:

0) new element
1) hello
2) qwerty

"some other data" is deleted. When you make your fifo 2 elements big it will will store the current and the previous number. You can compare these.
Last edited on
Topic archived. No new replies allowed.