# 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
17,450
4,701
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,605
2,472
And what's the rank of '*' and '#' with respect to actual digits?

4. ### WBahn Moderator

Mar 31, 2012
17,450
4,701
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,605
2,472
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
17,450
4,701
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)
3. {number = Keypad_key_Click();}
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
17,450
4,701
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
17,450
4,701
How many digits are displayed when you enter a number from the keypad?

12. ### ci139 Member

Jul 11, 2016
342
38
Google search |"mikroC" "decimal keypad routine" ram lcd| returned amongst other
Jul 29, 2015 Displaying a keypad input onto an LCD

i suggest you check both - if you already didn't know the next then: how to do such efficiently - is that you first loosely scan(with your eyes) the documents(webpages) for images , diagrams , numbers , words and/or phrases that could be describing the problem or the speciffic area of your interest and
►spend more time on the places where there is obvious match or very likely match to the subject you seek information on
so you 1-st identify "is relevant" and 2-nd verify "is relevant" !!! no identification means don't waste time on verification -- it's how you seach extensive databases such as internet