Question about how scanf works

This is a discussion on Question about how scanf works within the C Programming forums, part of the General Programming Boards category; Hi I am trying to make it so my program will accept only 2 input at a time if more ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Location
    Syracuse,NY
    Posts
    2

    Question about how scanf works

    Hi I am trying to make it so my program will accept only 2 input at a time if more then 2 are written. For example I want it so when i run main and it asks me to put a number and a string and another string, and I put something like foo bar cake it will ignore the cake completely and throw it away instead of what it currently does which would be take cake as the first input for the next run through.
    sample input and output is
    Please enter a string and another string
    foo bar
    String 1 = foo , String 2 = bar
    Please enter a string and another string
    foo bar cake
    String 1 = foo , String 2 = bar
    Please enter a string and another string
    foo bar
    String 1 = cake , String 2 = foo
    I thought what would work would be adding the fflush(stdin); after every run through but that didn't change anything. Any help would be appreciated.
    Code:
    import <stdio.h>
    int main ( int num_args, char *args[] ){
    
        while(!feof(stdin)){
            char str[20];
            char str2[20];
            printf("Please enter a string and another string\n");
            scanf("%s",str);
            scanf("%s",str2);
            printf("String 1 = %s , String 2 = %s\n",str,str2);
         }
    }
    Last edited by kabaniz; 01-06-2011 at 11:59 PM. Reason: typo

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    fflush(stdin) and using feof to control loops are bad. Google for why.

    What you are looking for is not doable in standard C. You will need to use platform-specific libraries.

    That's not trivial. So if you just started learning C, I would recommend leaving that for later.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Not doable?
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, sizeof buff, stdin ) ) {
            char str[20];
            char str2[20];
            if ( sscanf( buff, "%s %s", str, str2) == 2 ) {
                    printf("String 1 = %s , String 2 = %s\n",str,str2);
            }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    ... by mere mortals .

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    You can't do what you want with scanf() alone.

    What you'll need to do is read the string using fgets(), and then interpret it to extract strings you want. Logically, if you extract strings you want, and do nothing with any others the user might type in, they are effectively discarded.

    It is generally not a good idea to loop on feof() either. There are various FAQ's around that explain why.

    Rough code follows (note: it is written to illustrate the idea, not intended as perfection in coding);
    Code:
    #include the headers needed
    
    int main()
    {
          while (1)
          {
                 char str[20];
                 char str2[20];
                 char buffer[100];
                 fprintf(stdout, "Please enter a string and another string\n");
                 if (fgets(buffer, sizeof(buffer),  stdin) == NULL) break;  /*  break on error or EOF */
                 int strings_read = sscanf(buffer, "%19s %19s", str, str2))
                 if (strings_read == 1)
                 {
                       /*  Only 1 string input, nag user until we get the second  */
                       do
                       {
                           strings_read = 0;
                           fprintf(stdout, "You only entered one string.  Please input the second");
                       } while (fgets(buffer, sizeof(buffer), stdin) != NULL && (strings_read = sscanf(buffer, "%19s", str2)) != 1);
                        if (strings_read == 1)  ++strings_read;   /*  since this is the second string */
                 }
                 if (strings_read == 2)
                      fprintf(stdout, "String 1 = %s , String 2 = %s\n",str,str2);
                 else  /*  an error occurred */
                      break;
            }
       }
    }
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    1
    I recommend you to use gets() function instead of scanf in this case because scanf fuction will read only up to space.
    example:
    if you enter
    rahul dravid

    scanf() will read only upto rahul

  7. #7
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    gets() is vulnerable to a buffer overflow attack if not used correctly, and therefore it shouldn't be used.

    fgets() is safer.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by afiz View Post
    I recommend you to use gets() function instead of scanf in this case because scanf fuction will read only up to space.
    example:
    if you enter
    rahul dravid

    scanf() will read only upto rahul
    Code:
    #include <stdio.h>
    int main (void)
     {
        int words = 0;
        char First[20];
        char Last[20];
    
        puts("Enter your first and last name:");
        do
          {
             words = scanf ("%s %s",First,Last);
             if (words < 2)
               puts("First AND last name please");
           } while (words < 2);
    
       printf("Hello %s %s",First,Last);
    
       return 0;
    }

  9. #9
    Registered User
    Join Date
    Jan 2011
    Location
    Syracuse,NY
    Posts
    2
    Alright thanks, I'll rework it with fgets.

  10. #10
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Good idea-- good luck!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  2. scanf question
    By MMu in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2008, 10:15 AM
  3. Quick Question Regarding Scanf() & Phone Numbers!
    By weaveram in forum C Programming
    Replies: 3
    Last Post: 09-20-2007, 09:58 AM
  4. scanf question
    By jebster in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 06:39 AM
  5. error handling question
    By plexis in forum C Programming
    Replies: 3
    Last Post: 09-30-2002, 10:00 PM

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