![]() |
Re: function
Bill Cunningham wrote:
> > Ok. My goof. But if I wrote this. > > if (strcmp(pass,pass2)==0) {printf("success");} > else printf("error"); > > I can't figure out if and if else. I might need a little help with > conditional statements involving 2 possbilities. I'm assuming that you intended for the password to be "ded"... #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char pass[10]="ded", pass2[10]; printf("Authorization "); fflush(stdout); fgets(pass2,10,stdin); if(strcmp(pass,pass2)) { printf("error"); exit(EXIT_FAILURE); } printf("success"); return 0; } If you terminate the program immediately for an incorrect password, you won't need an 'else' for the success path. -- Morris Dovey DeSoto Solar DeSoto, Iowa USA http://www.iedu.com/DeSoto |
function
I have been having fits with this function.
Can anyone help me I think I need to use if and else if but I have rewritten it and got errors. I want a conditional in this program a choice between 2 options and only one works. I don't think I need the strcpy either. #include <stdio.h> #include <stdlib.h> #include <string.h> main(){ char pass[10]="ded", pass2[10]; printf("Authorization "); fflush(stdout); fgets(pass,10,stdin); strcpy(pass2,pass); if(strcmp(pass,pass2)==0) {printf("success"); exit(0);} else printf("error"); exit(EXIT_FAILURE);} |
Re: function
Bill Cunningham wrote:
> I have been having fits with this function. > Can anyone help me I think I need to use if and else if but I have rewritten > it and got errors. I want a conditional in this program a choice between 2 > options and only one works. I don't think I need the strcpy either. > > #include <stdio.h> > #include <stdlib.h> > #include <string.h> > > main(){ > char pass[10]="ded", pass2[10]; > printf("Authorization "); > fflush(stdout); > fgets(pass,10,stdin); > strcpy(pass2,pass); > if(strcmp(pass,pass2)==0) {printf("success"); exit(0);} > else > printf("error"); exit(EXIT_FAILURE);} > > As that you best you can do? I don't think anyone is going to bite this time. -- Ian Collins. |
Re: function
"Ian Collins" <ian-news@hotmail.com> wrote in message news:62s0m8F24fgk4U13@mid.individual.net... > Bill Cunningham wrote: >> I have been having fits with this function. >> Can anyone help me I think I need to use if and else if but I have >> rewritten >> it and got errors. I want a conditional in this program a choice between >> 2 >> options and only one works. I don't think I need the strcpy either. >> >> #include <stdio.h> >> #include <stdlib.h> >> #include <string.h> >> >> main(){ >> char pass[10]="ded", pass2[10]; >> printf("Authorization "); >> fflush(stdout); >> fgets(pass,10,stdin); >> strcpy(pass2,pass); >> if(strcmp(pass,pass2)==0) {printf("success"); exit(0);} >> else >> printf("error"); exit(EXIT_FAILURE);} >> >> > As that you best you can do? I don't think anyone is going to bite this > time. Guess so; maybe I can figure it out myself. I hope this doesn't bait trolls but I'm sure it's a if else question. Only talk C please. Bill |
Re: function
In article <fY3yj.9178$ES.7324@trnddc05>,
Bill Cunningham <nospam@nspam.com> wrote: > I have been having fits with this function. >Can anyone help me I think I need to use if and else if but I have rewritten >it and got errors. I want a conditional in this program a choice between 2 >options and only one works. I don't think I need the strcpy either. >#include <stdio.h> >#include <stdlib.h> >#include <string.h> > >main(){ > char pass[10]="ded", pass2[10]; > printf("Authorization "); > fflush(stdout); > fgets(pass,10,stdin); As you have initialized pass, my guess is that you would want to fgets into pass2 . > strcpy(pass2,pass); > if(strcmp(pass,pass2)==0) {printf("success"); exit(0);} Because of the strcpy, the two variables are going to contain the same thing, so the comparison will always work. > else > printf("error"); exit(EXIT_FAILURE);} -- "I will not approve any plan which is based on the old principle of build now and repair later." -- Walter Hickle |
Re: function
Ok. My goof. But if I wrote this.
if (strcmp(pass,pass2)==0) {printf("success");} else printf("error"); I can't figure out if and if else. I might need a little help with conditional statements involving 2 possbilities. Bill |
Re: function
In article <_l4yj.9185$ES.5227@trnddc05>,
Bill Cunningham <nospam@nspam.com> wrote: > Ok. My goof. But if I wrote this. >if (strcmp(pass,pass2)==0) {printf("success");} >else printf("error"); >I can't figure out if and if else. I'm not sure what the rest of your program looks like now. >I might need a little help with >conditional statements involving 2 possbilities. The portion inside the () after the 'if' is evaluated as an expression. If the expression evaluates to non-zero, the 'if' succeeds and the first statement afterwards (which might be a compound statement) is executed; if the expression evalutes to 0, the 'if' fails and the first statement after the 'else' (which might be a compound statement) is executed [if there is an 'else']. If you have only been able to trigger one of the cases, then it could be that the code above the 'if' is wrong, or it could be that the expression to be evaluated is wrong. I can't say which at the moment as we don't know your current code. -- "To the modern spirt nothing is, or can be rightly known, except relatively and under conditions." -- Walter Pater |
Re: function
[snip]
> If you have only been able to trigger one of the cases, then > it could be that the code above the 'if' is wrong, or it could > be that the expression to be evaluated is wrong. I can't say > which at the moment as we don't know your current code. Ok thanks I will work on it more and come back if I still can't get it right. Bill |
Re: function
Walter does an if statement between () have to be an arithmetic
statement? I am thinking I am trying to test if the value of the char type pass is "ded" or not. A string. So I am thinking strcmp() but if a string is "ded I want to branch to success. If the value of pass isn't "ded" I want to branch to something else. A failure. I am using fgets() to direct stdin. Bill |
Re: function
Might I have to change type with one of the data type changing functions
to change char pass[10] to an arithmetic type to do a comparison? The string "ded" either exists or doesn't and if needs to evaluate that. "Pass by value." someone once told me. So many hints that a mathematician designed this language. Bill |
| All times are GMT. The time now is 03:25 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.