# Why wont this work? bool equal

• 09-11-2003
gqchynaboy
Why wont this work? bool equal
Am i doing something majorly wrong? No matter what 2 integers i enter..its always true/Equal?

Code:

```  struct BigNum   {   unsigned int digit[MAX+1];   };   bool equal(const BigNum & value1, const BigNum & value2)       {     for (int i=0; i <=MAX; i++)     {  if(value1.digit[i] == value2.digit[i])     return true;     }     return false;```
• 09-11-2003
jlou
You are returning true if any of the digits match. You should return false if any of the digits don't match.
• 09-11-2003
gqchynaboy
that doesn't work
• 09-11-2003
JaWiB
Unfortunately you haven't initialized all of your array and your for loop keeps going even if the elements aren't initialized...You are probably running into the null terminating character which tells where your ... er sorry technical difficulties lol anyways your if statement probably sees a '\0' and a '\0' character in each of the arrays so it says its true. likewise, if you change your loop to check whether the arrays aren't equal or not, then it will read past the end of the array and get random values (which arent likely to be equal)[/edit]
• 09-11-2003
gqchynaboy
I have a init function but I'm not sure if im going the right way?
It doesn't compile...
Code:

```void init(BigNum & value) { for (int i=0; i <= MAX; i++)   {     value.digit[i] = 0;   } }  bool equal(const BigNum & value1, const BigNum & value2)     {     init(value1);     init(value2);     for (int i=0; i <=MAX; i++)     {  if(value1.digit[i] == value2.digit[i])     return false;     }     return true; }```
• 09-11-2003
JaWiB
for that to work you need to put the init definition below your equal function, or have a declaration at the top of the program. Also, i think 0 will be read as a null character in the array, so you might still need to change you loop so it stops if it reads a '\0' terminating character (however, I could be wrong)
• 09-11-2003
gqchynaboy
tried what you said..but still doesn't work...hummm...anyone else know?
• 09-11-2003
Sebastiani
Think about your logic here. First you returned true if you found A SINGLE MATCH, now you're returning false if that happens! Just loop thru the arrays, and if you find A SINGLE MISMATCH, return false.

Second:

for (int i=0; i <= MAX; i++)

That should be:

for (int i=0; i < MAX; i++)

ie:

if you declare:

#define MAX 5
char buff[MAX];

Then you have 5 elements, which index from 0-4.

So when i <= 5, you're out of bounds.
• 09-12-2003
gqchynaboy
u mean like this?

Code:

``` bool equal(const BigNum & value1, const BigNum & value2)     {     for (int i=0; i <MAX; i++)       if(value1.digit[i] != value2.digit[i])     return false; }```
• 09-12-2003
jlou
That's what I/we meant when saying to check for mismatch. I think Sebastiani didn't notice your first post where you make the array size MAX+1, so I think you can disregard his suggestion to use i < MAX. Also, make sure you return true if you make it out of the loop like you did before.

Here is your original loop with the changes we suggested. Please try to understand why this logic works better than your original.

Code:

```struct BigNum {     unsigned int digit[MAX+1]; }; bool equal(const BigNum & value1, const BigNum & value2) {     for (int i=0; i <=MAX; i++)     {         if(value1.digit[i] != value2.digit[i])             return false;     }     return true; }```
And finally, this only work if your numbers always have MAX digits. If they might be smaller, you need more logic.
• 09-12-2003
gqchynaboy