Thread: İf- else problem (new in c)

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    3

    İf- else problem (new in c)

    Code:
    #include <stdio.h>
    
    int  main()
    
    
    {
          
    char  a;
    long  b;
     
     printf("Name and password :");
     scanf ("%c %lb", &a, &b);
     
      if((a=='onur') && (b=="160897456")) 
                  
                  {
                  printf("hello sir");
                  }
                  else
                  {
                  printf("nice try");
                  }
           getchar();  
           getchar();
           getchar();
           getchar();
          
          
    }
    erors :
    multi-character character constant
    comparison is always false due to limited range of data type
    ISO C++ forbids comparison between pointer and integer

    What am ı doing wrong? And whats is your suggestions ? I think i dont understand properly . Which subjects do ı need to study ?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    (a=='onur')
    A single character is enclosed within single quotes (e.g. 'a').
    A string is enclosed within double quotes (e.g. "string").

    A string is a character array terminated with a null character ('\0').

    Strings: C Strings - Cprogramming.com

    Note:
    - 'onur' should be "onur"
    - your variable 'a' should be an array
    - you can't compare strings in C with == ... you need to use a function like "strcmp()"

    Code:
    (b=="160897456")
    An integer is not enclosed within double quotes; that is only for strings. You want to do:

    (b==160897456)

    Also:

    Code:
    scanf ("%c %lb", &a, &b);
    "%lb" should be "%ld".

    Code:
    getchar();  
    getchar();
    getchar();
    getchar();
    Not a good approach. Some more reading:

    FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com
    FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by Onur Berk Töre View Post
    Which subjects do ı need to study ?
    What C books have you read?

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    3
    Quote Originally Posted by FloridaJo View Post
    What C books have you read?

    I dont think you know that because book is Turkish, "A to Z C" /Kaan Aslan

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    3
    Quote Originally Posted by Matticus View Post

    Thank you for time

    And I think ı need re-read the book ( I am not totaly finished the book and actually this sample is exceed me, but ı must fit all concept in my head. Am ı wrong ? I will use this website for some extra information.)
    Last edited by Onur Berk Töre; 09-22-2014 at 09:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  3. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  4. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM