Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   function (http://www.velocityreviews.com/forums/t595236-function.html)

Morris Dovey 03-01-2008 03:13 AM

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

Bill Cunningham 03-01-2008 03:20 AM

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);}



Ian Collins 03-01-2008 03:26 AM

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.

Bill Cunningham 03-01-2008 03:29 AM

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



Walter Roberson 03-01-2008 03:40 AM

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

Bill Cunningham 03-01-2008 03:48 AM

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



Walter Roberson 03-01-2008 03:56 AM

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

Bill Cunningham 03-01-2008 04:11 AM

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



Bill Cunningham 03-01-2008 04:34 AM

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



Bill Cunningham 03-01-2008 04:47 AM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57