Re: function
- From: "Robbie Hatley" <lonewolf@xxxxxxxx>
- Date: Sat, 1 Mar 2008 03:59:37 -0800
"Bill Cunningham" wrote:
I have been having fits with this function. Can anyone help me?
What is your program supposed to do? You don't specify.
I think I need to use if and else if but I have rewritten
it and got errors.
What errors do you get? You don't specify.
I want a conditional in this program a choice between 2
options and only one works.
Sort of like a password, eh?
I don't think I need the strcpy either.
Then why not take it out and see what happens?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main(){
char pass[10]="ded", pass2[10];
Make your arrays at least 1 larger than the strings going into
them, to allow for the NUL terminators:
char pass[11]="ded", pass2[11];
printf("Authorization ");
fflush(stdout);
fgets(pass,10,stdin);
You're over-writing your "reference" password. You don't
want to do that. That last line should actually be:
fgets(pass2,10,stdin);
strcpy(pass2,pass);
Why on earth would you want to do that? You just destroyed
your "conditional" by making *ALL* passwords pass! That's
like having a computer with password regex ".*" (any character
string you please). So just get rid of that strcpy().
if(strcmp(pass,pass2)==0) {printf("success"); exit(0);}
else
printf("error"); exit(EXIT_FAILURE);}
It's better to format your code for readibility:
if (strcmp(pass,pass2)==0)
{
printf("success"); exit(0);
}
else
{
printf("error"); exit(EXIT_FAILURE);
}
There may be other errors there that I'm not seeing.
I don't have time to do your debugging for you.
I'll leave that as an exercise for the student.
But I think you'll find my tips above helpful.
--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
.
- References:
- function
- From: Bill Cunningham
- function
- Prev by Date: Re: array index and pointer, which is faster? - TPA
- Next by Date: Re: array index and pointer, which is faster? - TPA
- Previous by thread: Re: function
- Next by thread: Re: function
- Index(es):
Relevant Pages
|