Re: small string question
- From: Thad Smith <ThadSmith@xxxxxxx>
- Date: Sun, 06 Jan 2008 21:13:30 -0700
Bill Cunningham wrote:
Wait a minute I think I might've just caught my problem. It's in the function body where I'm making the error. Not assigning name[] to anything in the body. What about this.
char passw(char name[])
{char i=name;
As cr88192 noted, i is the wrong type. It should be char* instead of char.
....
if (strcmp(i,name2)==0) {puts"ok");}
Your code doesn't show a definition for name2, but if it is a string, the comparison is OK. Note that "string.h" must be included.
else if(strcmp(i,name2)!0) {puts("unequal. Try again"); return passw();}
That should be !=0 instead of !0. Also, you don't need to make the comparison again, since it exactly the opposite of the first one.
I started to show corrections in a rewritten version, but saw a more fundamental problem. I realized that by "return passw();" you were attempting to retry the password verification. The call is not passing the required parameter and is not returning the promised character value.
Why is the function defined to return a char? If you are thinking of the password, that would be returned in the array whose address is passed. If the code only exits with a verified password, you don't need any status. You might, however, have an option for the user to cancel, in which case you might want the return a status value.
The recursion you have written is a poor way to retry a password entry. It requires extra resources for recursion and also subjects the code to crashing if repeated failures are made. An appropriate code structure is an iterative loop, as shown in the following pseudocode:
do forever {
read password
read second copy
if (match) print "match" and return
else print "reenter"
}
--
Thad
.
- References:
- small string question
- From: Bill Cunningham
- Re: small string question
- From: Tomás Ó hÉilidhe
- Re: small string question
- From: Bill Cunningham
- small string question
- Prev by Date: Re: RotateLeft, what does it do?
- Next by Date: Re: Pointer conversions
- Previous by thread: Re: small string question
- Next by thread: Re: small string question
- Index(es):
Relevant Pages
|
|