CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

password program not verifying

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mohsin_hashmi



Joined: 07 Oct 2012
Posts: 12

View user's profile Send private message

password program not verifying
PostPosted: Sun Oct 07, 2012 10:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Oct 07, 2012 10:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Oct 07, 2012 10:56 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Oct 07, 2012 11:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Oct 07, 2012 12:00 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Oct 07, 2012 1:29 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Oct 07, 2012 1:51 pm     Reply with quote

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)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group