Thread: My if-else statement is flawed (only showing else code whatever input passed)

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    28

    My if-else statement is flawed (only showing else code whatever input passed)

    Program code =》
    Code:
    char c[20] ;
    gets(c);
    
    char match[20] = "Cat";
    
    if ( c == match) {
    puts("You entered Cat");
    } else {
    puts("Something else entered.");
    }



    Input: Cat
    Output: Something else entered.

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    156
    == doesn't work for char arrays, use strcmp instead.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    28
    Yeah sure i did that but when i do
    Code:
     
    
    strcmp(c, match)
    Inside if test expression i get an compilation error saying the function strcmp should have a prototype.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Add #include <string.h> to the top and then try again. Also, don't use gets() for the same reasons you learned in the other thread.

  5. #5
    Registered User
    Join Date
    Aug 2017
    Posts
    28
    It's still not working showing only the else part irregardless whatever the input is:
    Here is my code

    My if-else statement is flawed (only showing else code whatever input passed)-code-png

    Please take a look, and give me the solution. It would really be appreciated.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yeah. fgets() will leave the '\n' in the string where enter was pressed. You can remove it with additional code.

    See for example this page.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    You can't just throw in a library function call and expect it to work. You need to actually read about how the function works and use it correctly. For instance, if you read about strcmp you would learn that you need to include string.h to use it. Also, you would learn that it returns 0 if the strings are equal, so it's usually used like this:
    Code:
    if (strcmp(a, b) == 0)
        ; // strings are equal
    else
        ; // strings are not equal
    And you need to use fgets instead of the (no longer in the modern language) gets. But fgets leaves the newline in the string, so you'll either have to remove it or add a newline to your test string. A good way to remove it:
    Code:
    int len = strlen(s);   // strlen requires string.h
    if (s[len - 1] == '\n')
        s[--len] = '\0';
    BTW, your editor looks like it's from 30+ years ago. Are you a time traveller?
    Explode the sunlight here, gentlemen, and you explode the entire universe. - Plan 9 from Outer Space

  8. #8
    Registered User
    Join Date
    Aug 2017
    Posts
    28
    Quote Originally Posted by algorism View Post
    You can't just throw in a library function call and expect it to work. You need to actually read about how the function works and use it correctly. For instance, if you read about strcmp you would learn that you need to include string.h to use it. Also, you would learn that it returns 0 if the strings are equal, so it's usually used like this:
    Code:
    if (strcmp(a, b) == 0)
        ; // strings are equal
    else
        ; // strings are not equal
    And you need to use fgets instead of the (no longer in the modern language) gets. But fgets leaves the newline in the string, so you'll either have to remove it or add a newline to your test string. A good way to remove it:
    Code:
    int len = strlen(s);   // strlen requires string.h
    if (s[len - 1] == '\n')
        s[--len] = '\0';
    BTW, your editor looks like it's from 30+ years ago. Are you a time traveller?

    Then which IDE you suggest. Currently I am using Turbo C++ 3.0 Windows 7. And thanks for your help.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by kdushyant297 View Post
    Then which IDE you suggest. Currently I am using Turbo C++ 3.0 Windows 7. And thanks for your help.
    For windows - download free version of VS2017

    Downloads | IDE, Code, & Team Foundation Server | Visual Studio

    And choose Community Edition
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input showing as default values
    By worl4125 in forum C++ Programming
    Replies: 3
    Last Post: 12-01-2013, 11:27 AM
  2. Replies: 0
    Last Post: 12-04-2011, 11:29 PM
  3. How passed as an input parameter?
    By Siaw Ys in forum C Programming
    Replies: 3
    Last Post: 12-03-2011, 06:06 AM
  4. If statement is been passed over-Simple Opencv project
    By aprop in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2010, 12:05 PM
  5. stop input form showing on the screen
    By the_head in forum C Programming
    Replies: 7
    Last Post: 08-03-2002, 12:40 PM

Tags for this Thread