Thread: if-else

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21

    if-else

    Code:
    #include <stdio.h>
    main()
    {
    char a,sk8;
    printf("give password:");
    scanf("%c",&a);
    a=sk8;
    if (a=sk8)
    printf("well done\n");
    else
    printf("wrong password\n");
    }
    but can not understand else.anything i give to it,tells me well done.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    if (a=sk8)
    Use == for comparing, = for assignment.

    You've written (essentially)
    if ( (a=sk8) != '\0' )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21
    Code:
    #include <stdio.h>
    main()
    {
    char a,sk8;
    printf("give password:");
    scanf("%c",&a);
    a=sk8;
    if (a==sk8)
    printf("well done\n");
    else
    printf("wrong password\n");
    }
    ok.i change if but the same problem

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sk8harddiefast View Post
    Code:
    #include <stdio.h>
    main()
    {
    char a,sk8;
    printf("give password:");
    scanf("%c",&a);
    a=sk8;
    if (a==sk8)
    printf("well done\n");
    else
    printf("wrong password\n");
    }
    ok.i change if but the same problem
    What is sk8? If you dont initialize it, it'll be a garbage value.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    a=sk8;
    if (a==sk8)
    Where between these two statements do you expect a to change to something different from sk8?

    You have
    if ( 1 == 1 )
    for all that's worth.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And besides, you make sure a and sk8 are the same thing directly above that line, by assigning the current value of sk8 to a (discarding the typed-in value you had just read).

  7. #7
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21
    sk8 is the password.i initialize it char because is a character.sorry for my english but i dont know to speak verry well =[

  8. #8
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21
    so a=sk8; is not needed because i initialize it into if.wright?

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    yes. First initialize it
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sk8harddiefast View Post
    so a=sk8; is not needed because i initialize it into if.wright?
    It's not needed because it doesn't do what you want. You've already gotten a single character read into a, and sk8 is garbage (you presumably want to initialize it at some point). Although why you want a single-letter password I don't know.

  11. #11
    Registered User
    Join Date
    May 2009
    Location
    athens
    Posts
    21
    i dont know programming even C.i try to make something simple only for learn how to use if,char,int.the first steps =]

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need a good book and more reading. Your current understand is too weak for attempting any kind of programming with C.
    Also, main must return int. So in effect, your
    main()
    must be written as
    int main()
    And for heaven's sake, indent your code!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed