View previous topic :: View next topic |
Author |
Message |
mohsin_hashmi
Joined: 07 Oct 2012 Posts: 12
|
password program not verifying |
Posted: Sun Oct 07, 2012 10:08 am |
|
|
Hello !
I have written a program that asks for password. But even if i am entering the correct password "mohsin" it is still saying password not correct. Please help.
Code: |
#include <16F876A.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES LP //Low power osc < 200 khz
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES WRT_50% //Lower half of Program Memory is Write Protected
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include <string.h>
void main()
{
char k;
char name[6];
puts("**********************************");
puts(" Console Test ");
puts("**********************************");
putc('\n');
putc('\r');
while(1)
{
printf("Enter Password:");
gets(name);
puts(name);
if (name=="mohsin")
puts("Password is Correct");
else
puts("Password is Incorrect");
}
} |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Oct 07, 2012 10:42 am |
|
|
Of course it doesn't work.
The CCS manual shows how you could perform password function.
For starters your array [name] isn't big enough.
String arrays need an extra space for a terminating zero.
It's advisable to add errors to the #use RS232.
Mike |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sun Oct 07, 2012 10:56 am |
|
|
You also need to look up the string functions in the user manual for comparing strings. In addition to Mikes comment on the buffer length, part of the error checking should be to verify the entered string does not overflow the input buffer - one of the first hacks people try to break into systems is to overflow input buffers.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
mohsin_hashmi
Joined: 07 Oct 2012 Posts: 12
|
|
Posted: Sun Oct 07, 2012 11:17 am |
|
|
I have tried the example in the CCS C manual but same problem.
Code: |
char string[30];
char temp[30];
strcpy(temp,"mohsin");
printf("Password: ");
gets(string);
if(strcmp(string,temp))
printf("OK");
else
printf("Wrong");
|
|
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sun Oct 07, 2012 12:00 pm |
|
|
A little basic debugging - have you tried printing out both the test string and the string you received to see if they are the same ? Printf(thingies) is your friend.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Oct 07, 2012 1:29 pm |
|
|
Another tip for debugging is not only to test for the answer you are looking for, but also try to see what happens when you give a wrong input. Sometimes this gives you a very helpful hint about what is wrong... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Oct 07, 2012 1:51 pm |
|
|
Quote: | if(strcmp(string,temp))
printf("OK");
else
printf("Wrong");
|
Same old problem. Doesn't read the manual. Doesn't read about what
is the return value for strcmp() for a match.
http://www.ccsinfo.com/forum/viewtopic.php?t=47945&start=2
And using Proteus for sure:
Quote: |
#include <16F876A.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES LP //Low power osc < 200 khz
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES WRT_50% //Lower half of Program Memory is Write Protected
#use delay(clock=20000000) |
|
|
|
|