compare two numbers

Thread Starter

samjabr

Joined Jul 5, 2016
4
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)

{number = Keypad_key_Click();}

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==10) number = '6';
if(number==11) number = '9';
if(number==12) number = '#';
Lcd_Chr(2,i+1, number);
}


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

WBahn

Joined Mar 31, 2012
32,829
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.
 

WBahn

Joined Mar 31, 2012
32,829
And what's the rank of '*' and '#' with respect to actual digits?
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.
 

Thread Starter

samjabr

Joined Jul 5, 2016
4
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.
 

WBahn

Joined Mar 31, 2012
32,829
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.
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:

C:
while (number == 0)
{number = Keypad_key_Click();}
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

C:
if(number==9) number = '3';
if(number[i]==10) number[i] = '6';
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?
 

ci139

Joined Jul 11, 2016
1,989
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
 
Top