I have entered the password 1234. But it doesn't display "correct password" on LCD. Why is it?
Perhaps, because you are retrieving and storing the password in an array and then comparing the array name, pointer constant value, to numerical value 1234.
The following are equivalent:
Code:
int8 * ptr;
ptr = &password_array[0];
ptr = password_array
The constant value contained in password_array is a pointer (memory address) which points to the first element of the array of the same name.
**broken link removed**
Code:
int8 password_array[MAX_PASSWORD_SIZE +1];
....
....
kbd_get_string(password_array, MAX_PASSWORD_SIZE); // password_array points to the first element
....
....
if([COLOR="#FF0000"]password_array == 1234[/COLOR]) // pointer constant value is compared to 1234
{
lcd_putc("\fCorrect Password\n");
}
As the source for the kbd_get_string() routine is not available, it is difficult to advise you on the correct method of comparison.
If the kbd_get_string() does indeed retrieve the numerical values and store them as ASCII characters into seqential elements of the password_array, then you should use the strcmp() routine to perform the comparison.
C library function - strcmp()
Code C - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| #include "18F4520.h"
#fuses XT
#use delay(clock = 4000000)
#include "flex_lcd.c"
#include "flex_kbd.c"
#include "kbd_buffer.c"
#include "string.h"
#define MAX_PASSWORD_SIZE 8
//==================================
void main()
{
char password_array[MAX_PASSWORD_SIZE +1];
char correct_password[] = "1234";
lcd_init();
kbd_init();
kbd_buffer_init();
while(1)
{
lcd_putc("\fEnter Password:\n");
// Get the password from keypad input by the user. The password
// can be from 1 to 8 digits.
// The user must press '#' key to exit the password entry screen.
// The user can press '*' key to backspace over chars.
kbd_get_string(password_array, MAX_PASSWORD_SIZE);
// Display the password that we just got.
lcd_putc("\fPassword is:\n");
printf(lcd_putc, "%s", password_array);
delay_ms(3000);
if(strcmp(password_array, correct_password) == 0)
{
lcd_putc("\fCorrect Password\n");
}
}
} |
Please post the source code for the kbd_get_string() routine for more definitive advice.
I'm not familiar with CCS and its builtin types, so I'm assuming there is a char type available
Hope this clears a few things up,
BigDog