# compare two numbers

Discussion in 'Programmer's Corner' started by samjabr, Jul 7, 2016.

1. ### samjabr Thread Starter New Member

Jul 5, 2016
4
0
Hello dears,
I'm programming with mikroC and I have a problem ; I need to compair two numbers :
a number how displayed in LCD (analogic input) and another number write using the keypad.

for clarification, I used (char a3,a2,a1) for display value of analogic input : lcd_chr(1,1,a3+48); lcd_chr( 1,2,a2+48); lcd_chr(1,3,a1+48);
a3: hundreds a2:tens a1: units

for the keypad I used this programme:
for(i=0;i<2;i++){
while (number == 0)

if(number==1) number = '1';
if(number==2) number = '4';
if(number==3) number = '7';
if(number==4) number = '*';
if(number==5) number = '2';
if(number==6) number = '5';
if(number==7) number = '8';
if(number==8) number = '0';
if(number==9) number = '3';
if(number[i]==10) number[i] = '6';
if(number[i]==11) number[i] = '9';
if(number[i]==12) number[i] = '#';
Lcd_Chr(2,i+1, number[i]);
}

In other words I need to compair { lcd_chr(2,i+1,number[i]) with a3 a2 a1 (the whole number) }
thanks all .[/i][/i][/i][/i][/i][/i][/i][/i]

2. ### WBahn Moderator

Mar 31, 2012
18,085
4,917
You code makes very little sense.

Why are some of your comparisons:

if(number==3) number = '7';

and others

if(number==10) number = '6';

You don't declare the variable 'number', so we can't tell if it is an integer, or a char, or an array. You use it as all three, so two of them are clearly wrong.

Then you appear to want to compare two three digit numbers, but your loop that reads the keyboard only runs twice, once for i = 0 and once for i = 1.

3. ### joeyd999 AAC Fanatic!

Jun 6, 2011
2,756
2,916
And what's the rank of '*' and '#' with respect to actual digits?

4. ### WBahn Moderator

Mar 31, 2012
18,085
4,917
My guess is that that part of the code just reflects the return codes for the entire keypad (a 4x3, not a 4x4) and that he is assuming (generally bad assumption) that the user is only going to actually press one of the numeric keys.

5. ### joeyd999 AAC Fanatic!

Jun 6, 2011
2,756
2,916
Which was my point, though not explicitly made.

6. ### samjabr Thread Starter New Member

Jul 5, 2016
4
0
of curse I declared all variables (char number[3]; int i)...this is just a litle part of the programme ; the part do I have probleme .. thanks everybody.

7. ### WBahn Moderator

Mar 31, 2012
18,085
4,917
Of course you declared them -- but you didn't let US know WHAT you declared them AS, and we couldn't determine that from your code snippet since you USE it as you would three different types.

Now we know that 'number' is a char array.

So think of the following code fragment:

Code (C):
1.
2. while (number == 0)
4.
This is saying that while 'number' is a NULL pointer, assign the output of Keypad_key_Click() to the pointer 'number'.

But if you declare 'number' as a char array, then it will NOT be NULL, it will equal the address of the number[0]. It will also be a constant and most compilers won't let you overwrite it.

So just this code here is going to cause you all kinds of grief.

Then consider

Code (C):
1.
2. if(number==9) number = '3';
3. if(number[i]==10) number[i] = '6';
4.
In the first line you are checking if the pointer 'number' is equal to memory address 9 and, if it is, setting it to memory address 51 (assuming your character set is ASCII). What's stored at memory location 51?

In the second line, you are checking if the value stored in the ith element of the array pointed to by 'number' is equal to 10 and, if it is, overwriting it with the ASCII code for '6'.

Why the difference?

8. ### samjabr Thread Starter New Member

Jul 5, 2016
4
0
there is no difference bought are correct

9. ### WBahn Moderator

Mar 31, 2012
18,085
4,917
Both are correct?

Do you understand the difference between a pointer to an array of char variables and a char variable?

10. ### samjabr Thread Starter New Member

Jul 5, 2016
4
0
No, but it is give me the same result ;display a digital number.

11. ### WBahn Moderator

Mar 31, 2012
18,085
4,917
How many digits are displayed when you enter a number from the keypad?

Jul 11, 2016
341
39