Comparing int arrays

Apr 5, 2012 at 4:47pm
I am creating a program to 1) Generate a random 10 digit account number. 2) Have a user enter an account number that needs to match the generated account number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void account::GenerateAccountNumber()
{
	cout<<"Your account number is: ";

	for(int i=0; i<10; i++)
	{
		accountnumber[i+1] = rand() % 9;
		cout<<accountnumber[i+1];
	}
	cout<<endl;
}
.
.
.
void account::AccountNumberCompare(int actual[11], int guess[11])
{
	for(int i =0; i<12; i++)
	{
		if(actual[i] == guess[i])
			CorrectAccountNumber *= 1;
		else
			CorrectAccountNumber *= 0;
	}
}


CorrectAccountNumber always returns 0.

I'm declaring it with
AccountNumberCompare(accountnumber, accountguess);
With the declaration int accountnumber[11], accountguess[11];
Last edited on Apr 5, 2012 at 5:08pm
Apr 5, 2012 at 4:53pm
If you have a ten digit account number, why are you comparing 11 numbers?


Also, the body of the for is wrong. As long as the last number processed is correct (even if all previous numbers in the 'guess' were incorrect,) CorrectAccountNumber will be 1.
Apr 5, 2012 at 5:08pm
Sorry, those are supposed to be *=, not just =. Still doesn't work.
Apr 5, 2012 at 5:11pm
If you have a ten digit account number, why are you comparing 11 numbers?


And, why, when you're generating the account number do you start at index i+1?
Last edited on Apr 5, 2012 at 5:12pm
Apr 5, 2012 at 5:17pm
You get a stack overflow otherwise.
Apr 5, 2012 at 5:23pm
You get a stack overflow otherwise.

Why would that be the case?

You're saying you want to generate a 10-digit number, have an array with 11 elements and are comparing 12 digits. Choose one.
Apr 5, 2012 at 5:27pm
That makes no sense.

In your generation function you generate 10 numbers, beginning at index 1.

In your compare function you compare 11 numbers beginning with index 0, so if the values at index 0 ever differ, CorrectAccountNumber can't be 1, even if all the actual numbers match.
Topic archived. No new replies allowed.