Odd Behavior in If Statement

I have been teaching myself c++ for a few weeks now and I've never had any problem I was unable to solve on my own until now. This program seems so simple but I just don't understand the way it is behaving.

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
#include <iostream>
using namespace std;
bool uniquedigits(int x);
bool arraysearch (int x, int y[], int z);

int main()
{   
    for (int x = 123456789, count = 0;count < 100; x++) {
        if (uniquedigits(x)) {
            cout<<x<<endl;
            count++;
            cout<<count<<endl;
        }
    }
}

bool uniquedigits(int x)
{
    int a = 0;
    int b = 0;
    int used[10];
    string temp = itos(x);
    int size = temp.length();
    if (size == 9) {
        used[0] = 0;
        b++;
    }
    while (a < size) {
        int digit = temp.at(a)-'0';
        if (arraysearch(digit, used, b)) {
            return false;
        }
        else {
            used[b]=digit;
        }
        b++;
        a++;
    }
    return true;
}
bool arraysearch (int x, int y[], int z) 
{
    for (int a = 0; a <= z; a++) {
        if (x == y[a]) {
            return true;
        }
    }
    return false;
}


When I run this program I get this output:

123456789
1

and the program continues forever. At first I thought my uniquedigits function was the problem but when I add a second cout to my program:
1
2
3
4
5
6
7
8
9
10
11
int main()
{   
    for (int x = 123456789, count = 0;count < 100; x++) {
        if (uniquedigits(x)) {
            cout<<x<<endl;
            count++;
            cout<<count<<endl;
        }
        cout<<x<<"\t"<<count<<endl;
    }
}

It runs as expected:

123456789
1
123456789	1
123456790	1
123456791	1
123456792	1
123456793	1
123456794	1
123456795	1
123456796	1
123456797	1
123456798
2
123456798	2
123456799	2
123456800	2
123456801	2
123456802	2
123456803	2
123456804	2
123456805	2
123456806	2
123456807	2
123456808	2
123456809	2
123456810	2
123456811	2
123456812	2
123456813	2
123456814	2
123456815	2
123456816	2
123456817	2
123456818	2
123456819	2
123456820	2
123456821	2
123456822	2
123456823	2
123456824	2
123456825	2
123456826	2
123456827	2
123456828	2
123456829	2
123456830	2
123456831	2
123456832	2
123456833	2
123456834	2
123456835	2
123456836	2
123456837	2
123456838	2
123456839	2
123456840	2
123456841	2
123456842	2
123456843	2
123456844	2
123456845	2
123456846	2
123456847	2
123456848	2
123456849	2
123456850	2
123456851	2
123456852	2
123456853	2
123456854	2
123456855	2
123456856	2
123456857	2
123456858	2
123456859	2
123456860	2
123456861	2
123456862	2
123456863	2
123456864	2
123456865	2
123456866	2
123456867	2
123456868	2
123456869	2
123456870	2
123456871	2
123456872	2
123456873	2
123456874	2
123456875	2
123456876	2
123456877	2
123456878	2
123456879
3
123456879	3
123456880	3
123456881	3
123456882	3
123456883	3
123456884	3
123456885	3
123456886	3
123456887	3
123456888	3
123456889	3
123456890	3
123456891	3
123456892	3
123456893	3
123456894	3
123456895	3
123456896	3
123456897
4

I cut off most of it but you can see that the uniquedigits function is working. Even stranger is that the cout in the if statement also shows up:

123456797	1
123456798
2
123456798	2

I'm not sure if I missed some basic rule for using cout, but I would really appreciate any help in understanding why placing a second cout statement makes the first one work.

I am using Xcode on Mac OSX is it matters.

Thanks in advance!

Edit:
I got a reply in my email, but I don't see it here yet. Not sure if this forum has a delay or it got deleted, but I'll answer anyway. I didn't explain this because I was more focused on the cout statements, but the unique digits function is meant to work with full 10 digit numbers.

So 123456789 in main should be read as 0123456789.
Hence the reason for this code:
1
2
3
4
if (size == 9) {
        used[0] = 0;
        b++;
    }
Last edited on
There is a problem when you call arraysearch. used[b] will be accessed inside that function which is an uninitialized value. I think it should be a < z instead of a <= z.
Last edited on
Ah, that does fix it. Thank you very much.

At this point it is pure curiosity, but could you explain why adding a second cout had such an odd effect in the original code?
Using uninitialized values invokes undefined behaviour which means there are no guarantees what will happen. Avoid undefined behaviour at all cost.
Alright, thanks again
Topic archived. No new replies allowed.