what is wrong with my if statement

This is a discussion on what is wrong with my if statement within the C Programming forums, part of the General Programming Boards category; i cant execute this query, and i would like some help Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> main() { char user,pass,run; scanf("%c",&user); ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    62

    what is wrong with my if statement

    i cant execute this query, and i would like some help

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    main()
    {
          char user,pass,run;
          scanf("%c",&user);
          scanf("&c",&pass);
          if(user=="joker" && pass=="t");
           (
           printf("right input")
           )      
          else
          (
          printf("incorrect password");
          )
    
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    if(user=="joker" && pass=="t");
    This code shouldn't even compile!
    You're trying to compare a single character to a string (which wouldn't even work, even if user was a string), then you compare pass with "t" which should use single-quotes 't' since it's comparing just one character.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    This is part of your problem.
    Code:
     if(user=="joker" && pass=="t");
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User t3chn0n3rd's Avatar
    Join Date
    Dec 2007
    Location
    kansas city
    Posts
    25

    if statement

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    main()
    {
          char user,pass,run;
          scanf("%c",&user);
          scanf("&c",&pass);
          if(user=="joker" && pass=="t");
           (
           printf("right input")
           )      
          else
          (
          printf("incorrect password");
          )
    
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You didn't change anything there, so no wonder it still doesn't work. Also note that "&c" is not a valid format string for scanf -- or rather, it is, but it doesn't do anything except look for a literal ampersand a literal letter c.

    And as previously mentioned, user=="joker" compares things of different types, as does pass=="t", and since the if statement is empty ";" the else doesn't match with anything.

    Edit: Wait, that wasn't the original poster. Oh ... kay. Anyway, all of the above is still true at least.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    Someone has studying to do.
    Lesson 1) Strings are char arrays. String is NOT one char. You can't read strings into one char. That's mega buffer overrun. http://cpwiki.sf.net/Buffer_overrun
    Lesson 2) You can't compare strings with == in C, because it will just compare two pointers, due to how arrays work. Use strcmp.
    Lesson 3) ALL lines MUST end with a semicolon ;
    Lesson 4) If statements or other "block statements" such as functions that has { and }, should NOT end with a semicolon ( ; ).
    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.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    strings can compare...............

    Quote Originally Posted by joker_tony View Post
    i cant execute this query, and i would like some help

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    main()
    {
          char user,pass,run;
          scanf("%c",&user);
          scanf("&c",&pass);
          if(user=="joker" && pass=="t");
           (
           printf("right input")
           )      
          else
          (
          printf("incorrect password");
          )
    
    }
    Hai
    String comparision method is wrong...strings can be compared only using standard
    library function strcmp(string1, string2)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 12:26 AM
  2. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 04:06 AM
  3. What's wrong with my success statement??
    By cool_dude07 in forum C Programming
    Replies: 7
    Last Post: 07-21-2007, 11:22 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Something wrong with this if statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-30-2002, 05:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21