wrong output on input

This is a discussion on wrong output on input within the C Programming forums, part of the General Programming Boards category; hey, i've got (in my eyes) this weird little problem: Code: #include <stdio.h> int main() { char day[10]; printf("give a ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    67

    wrong output on input

    hey, i've got (in my eyes) this weird little problem:

    Code:
    #include <stdio.h>
    
    int main()
    {
        char day[10];
            
        printf("give a day: ");
        scanf("%s", day);
        
        
        if ((day == "saturday") || (day == "sunday"))
        {
            
            printf("\nIts weekend\n");
        }
        else
        {
            printf("\n its a weekday");
        }
            
    }
    no matter what day I input it always returns weekday.



    some got an idea how this happens because i really dont.
    thanx
    Last edited by TeQno; 06-05-2003 at 03:36 AM.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    You can't use '==' to compare strings (what you are actually doing is comparing to char * pointers which are never equal since they point to different things... so you always get an answer of false... so you always think its a week day)

    Try strcmp() or strncmp() which are defined in the header string.h

    edit: Salem beat me to it!
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    67

    ah

    ah thanx guys i didnt know i couldnt use == for this thing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  2. Using SSCANF input string output two variables
    By kevndale79 in forum C Programming
    Replies: 9
    Last Post: 10-02-2006, 03:57 PM
  3. Can we input negative numbers in the output windiw
    By hitesh1511 in forum C Programming
    Replies: 1
    Last Post: 08-22-2006, 01:07 AM
  4. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  5. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 02:11 AM

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