Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > function

Reply
Thread Tools

function

 
 
Morris Dovey
Guest
Posts: n/a
 
      03-01-2008
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
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      03-01-2008
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);}


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-01-2008
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.
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      03-01-2008

"Ian Collins" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      03-01-2008
In article <fY3yj.9178$ES.7324@trnddc05>,
Bill Cunningham <(E-Mail Removed)> 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
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      03-01-2008
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


 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      03-01-2008
In article <_l4yj.9185$ES.5227@trnddc05>,
Bill Cunningham <(E-Mail Removed)> 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
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      03-01-2008
[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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      03-01-2008
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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      03-01-2008
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments