Thread: Multiple words in if statement

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Perhaps something like:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char apple[100];
        char goat[100];
        scanf("%99s", goat);
    
        // Get rid of any characters remaining in the input buffer.
        while ( getchar() != '\n' );
    
        if(strcmp(goat, "something") == 0)
        {
            scanf("%99[^\n]", apple);
            // Or use fgets().
            // fgets(apple, 100, stdin);
        }
    
        printf("%s\n", apple);
    
        return 0;
    }
    You need to remove the trailing new line character before you use either fgets() or the scanf().

    Jim

  2. #17
    Registered User
    Join Date
    Dec 2016
    Posts
    8
    I mean that I can press space between two words

  3. #18
    Registered User
    Join Date
    Dec 2016
    Posts
    8
    Quote Originally Posted by jimblumberg View Post
    Perhaps something like:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char apple[100];
        char goat[100];
        scanf("%99s", goat);
    
        // Get rid of any characters remaining in the input buffer.
        while ( getchar() != '\n' );
    
        if(strcmp(goat, "something") == 0)
        {
            scanf("%99[^\n]", apple);
            // Or use fgets().
            // fgets(apple, 100, stdin);
        }
    
        printf("%s\n", apple);
    
        return 0;
    }
    You need to remove the trailing new line character before you use either fgets() or the scanf().

    Jim
    It works! Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. str_cmp string comparison with multiple words
    By Llarn in forum C Programming
    Replies: 11
    Last Post: 09-16-2013, 02:25 PM
  2. Replies: 7
    Last Post: 10-01-2010, 04:09 PM
  3. Question on grepping multiple words in a line
    By Overworked_PhD in forum Linux Programming
    Replies: 6
    Last Post: 04-11-2009, 11:12 PM
  4. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  5. Website the defines multiple words at once?
    By digdug4life in forum Tech Board
    Replies: 5
    Last Post: 12-16-2006, 11:39 AM

Tags for this Thread