function

Discussion in 'C Programming' started by Morris Dovey, Mar 1, 2008.

  1. Morris Dovey

    Morris Dovey Guest

    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
    Morris Dovey, Mar 1, 2008
    #1
    1. Advertising

  2. 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);}
    Bill Cunningham, Mar 1, 2008
    #2
    1. Advertising

  3. Morris Dovey

    Ian Collins Guest

    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.
    Ian Collins, Mar 1, 2008
    #3
  4. "Ian Collins" <> wrote in message
    news:...
    > 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
    Bill Cunningham, Mar 1, 2008
    #4
  5. In article <fY3yj.9178$ES.7324@trnddc05>,
    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);


    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
    Walter Roberson, Mar 1, 2008
    #5
  6. 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
    Bill Cunningham, Mar 1, 2008
    #6
  7. In article <_l4yj.9185$ES.5227@trnddc05>,
    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'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
    Walter Roberson, Mar 1, 2008
    #7
  8. [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, Mar 1, 2008
    #8
  9. 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, Mar 1, 2008
    #9
  10. 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
    Bill Cunningham, Mar 1, 2008
    #10
  11. In article <915yj.1231$6R.955@trnddc04>,
    Bill Cunningham <> wrote:
    > Walter does an if statement between () have to be an arithmetic
    >statement?


    The part in the () in an 'if' statement must be an expression.
    Every expression has a value (by definition.) The value that
    results from the expression will be compared to 0, and the
    first statement will be chosen if it is -not- 0 and the 'else'
    statement (if any) will be chosen if the expression -is- 0.

    If you code the expression with one of the logical operators,
    such as if (x > 3) then it is fairly obvious what the
    logical result of the expression is -- it will be either true or false.
    The logical operators all return 1 to mean true and 0 to mean false.

    If you code the expression with an arithmetic value but no logical
    operator, then remember the implicit comparison to 0. For example
    if you code if (x) then it is the same as if (x != 0).
    This is the case whether x is some kind of integer or some kind
    of floating point value -- it is either 0 or it isn't.

    The above two paragraphs might sound like two different rules, but
    they are really the same rule: does the result of the expression
    equal 0 or does it not? The fact that a logical operator
    happens to produce 1 for true does not matter to C: what is important
    to C's 'if' statements is that the result is not 0.

    What is tested in the 'if' statement can also be a pointer of
    some kind. This involves a special case: when x is a pointer,
    if (x) is considered true if the pointer is not NULL, and
    considered false if the pointer -is- NULL. In many systems,
    NULL -is- 0 internally, but that is not true in all implementations;
    whatever internal value or values that the implementation uses
    to represent NULL, the implementation is responsible for testing
    against those internal values for the if (x) case where x is a pointer,
    which is the same situation as if if (x != NULL) had been coded.


    >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.


    strcmp(pass, "ded") will return an arithmetic value that will be
    non-zero if the strings are not the same, and will be 0 if the
    strings -are- the same. Because 0 is returned if they -are- the
    same, the code if (strcmp(pass,"ded") == 0) will end up
    comparing 0 to 0 if the strings -are- the same, and since 0 == 0
    the test would be true and the first statement after the 'if' would
    be executed. Therefore your originally posted code

    if (strcmp(pass,pass2) == 0)

    was exactly the right kind of test to make to check to see if
    the two strings were the same. The original code was marred by
    the strcpy() that you had above the test, which had the effect
    of forcing the two strings to be the same, so because of the
    strcpy(), the two would always compare equal in strcmp(), so
    0 would be returned by strcmp, that 0 would be tested with == 0
    because you coded that, and that would be true, so only the first
    statement after the 'if' could be reached. If you did not have
    the strcpy() in there, that problem would not have been present --
    but you would have still had the problem that you were fgets'ing
    into your initialized variable rather than into your other variable.
    --
    "Pray do not take the pains / To set me right. /
    In vain my faults ye quote; / I wrote as others wrote /
    On Sunium's hight." -- Walter Savage Landor
    Walter Roberson, Mar 1, 2008
    #11
  12. Bill Cunningham wrote:
    > "Ian Collins" <> wrote in message
    > news:...
    >> 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.


    Okay, then C the FAQ, please. ;)

    <SCNR>

    Uli
    Ulrich Eckhardt, Mar 1, 2008
    #12
  13. "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
    Robbie Hatley, Mar 1, 2008
    #13
  14. "Bill Cunningham" <> writes:

    > [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.


    If your current version is similar to the original, then the problem
    you have is that fgets retains the terminating newline (the character
    that marks the end of a line of input). You need either to remove
    this newline from pass2 or test for strcmp(pass2, "ded\n") == 0.

    --
    Ben.
    Ben Bacarisse, Mar 1, 2008
    #14
  15. This should work.

    Best Regards,
    Karthigan.

    ************************************************************
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main(){

    char pass[10]="ded", pass2[10];

    printf("Authorization ");
    fflush(stdout);

    gets(pass2);

    if(strcmp(pass,pass2)==0) {printf("success"); exit(0);}

    else {printf("error"); exit(EXIT_FAILURE);}

    return 0;
    }

    *************************************************************


    On Fri, 29 Feb 2008 21:20:43 -0600, 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);}
    >
    >




    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
    Karthigan Srinivasan, Mar 1, 2008
    #15
  16. Karthigan Srinivasan said:

    > This should work.


    <snip>

    > gets(pass2);


    This is a really, really, reall bad idea. Never, *ever* use gets(), because
    it has no way to protect your buffer against being overrun by excessive
    data.

    <snip>

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Mar 1, 2008
    #16
  17. > If your current version is similar to the original, then the problem
    > you have is that fgets retains the terminating newline (the character
    > that marks the end of a line of input). You need either to remove
    > this newline from pass2 or test for strcmp(pass2, "ded\n") == 0.
    >

    Wow. Thanks Ben. I didn't know strcmp took a literal string. So much for
    my knowledge.

    Bill
    Bill Cunningham, Mar 1, 2008
    #17
  18. "Morris Dovey" <> wrote in message
    news:...

    > I'm assuming that you intended for the password to be "ded"...


    Yes.

    > #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.
    >
    Bill Cunningham, Mar 1, 2008
    #18
  19. "Bill Cunningham" <> writes:
    > 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);}


    Do you pay a per-character fee for using whitespace in your source
    code?

    I think your code layout is making it difficult for you to see what
    your program is actually doing. It certainly makes it difficult for
    me to read it. In particular, your style of hiding each closing brace
    character '}', combined with your insistence on putting multiple
    statements on a line, is probably the cause of your current confusion.
    Using only a single column for indentation also makes your code
    difficult to read.

    Here's your program again, with no changes other than the addition of
    whitespace (I would make several other changes as well, but I haven't
    done so here). It should be enough to show you what the problem is.

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

    Code layout matters.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 1, 2008
    #19
  20. Keith Thompson <> writes:
    > "Bill Cunningham" <> writes:
    >> 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);}

    >
    > Do you pay a per-character fee for using whitespace in your source
    > code?
    >
    > I think your code layout is making it difficult for you to see what
    > your program is actually doing. It certainly makes it difficult for
    > me to read it. In particular, your style of hiding each closing brace
    > character '}', combined with your insistence on putting multiple
    > statements on a line, is probably the cause of your current confusion.
    > Using only a single column for indentation also makes your code
    > difficult to read.
    >
    > Here's your program again, with no changes other than the addition of
    > whitespace (I would make several other changes as well, but I haven't
    > done so here). It should be enough to show you what the problem is.
    >
    > #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);
    > }
    >
    > Code layout matters.


    My apologies, I didn't read the code closely enough.

    I has assumed that you wanted both statements:
    printf("error");
    exit(EXIT_FAILURE);
    to be in the else clause, which would require braces surrounding both
    of them.

    Since the if clause:
    printf("success");
    exit(0);
    always exits, it doesn't matter *in this particular case*. The
    behavior of the program is the same whether the exit(EXIT_FAILURE); is
    within the else clause, or following and separate from the entire
    if/else statement.

    But your original code almost completely obscures this point. Your
    problem isn't what I thought it was, but your coding style still makes
    your code difficult to read. And IMHO the exit(EXIT_FAILURE); clause
    *should* be part of the else clause.

    Here's another version of your program, again with no changes other than
    whitespace:

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

    It's not as legible as your original version, but it's not all that
    much worse.

    Fixing this problem (and, among other things, showing that you can
    accept good advice) will make it more likely that my response to your
    next question will be anything more than "Please clean up this mess if
    you expect me to read it".

    Furthermore, all you said about your problem is that you "got errors".
    Telling us *what* errors you got, as well as what you expected the
    program to do, would make it much easier to help you.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 1, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,011
    Evan Carew
    Jan 21, 2004
  2. komal
    Replies:
    6
    Views:
    1,406
    msalters
    Jan 25, 2005
  3. Replies:
    2
    Views:
    895
    Bengt Richter
    Aug 1, 2005
  4. Giannis Papadopoulos

    Function pointer to void function and int function

    Giannis Papadopoulos, Sep 5, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,216
    Barry Schwarz
    Sep 5, 2005
  5. weafon
    Replies:
    1
    Views:
    290
    Diez B. Roggisch
    Jul 14, 2009
Loading...

Share This Page