User input gets skipped

This is a discussion on User input gets skipped within the C Programming forums, part of the General Programming Boards category; Well i don't have code on me, but I sometimes come across a situation in the past where my program ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    25

    User input gets skipped

    Well i don't have code on me, but I sometimes come across a situation in the past where my program skips the user input, such as a gets line or an fgets line. I'm wondering why it skips this?
    is there a general reason behind this?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Next time post code. This is such a general question, that if it wasn't such a common issue, nobody would have the slightest idea what you're talking about.

    The issue is probably because you're mixing and matching different forms of input functions that behave slightly differently. One example would be if you tried to read an integer via scanf() and then a string via fgets().

    If you entered a number such as 45, the input buffer would look like this:

    Code:
    '4', '5', '\n'
    scanf() would read the 45 and leave the '\n' in the buffer, resulting in an input buffer that looks like this:

    Code:
    '\n'
    fgets() would then come along and look for a string. It is built, however, to stop on either of two conditions:

    1. It reaches the limit on the chars read, set by the caller (in the 2nd argument).
    2. It finds a '\n' in the buffer.


    Since fgets() finds a '\n', it takes it, stores it, and returns right away. The user is not prompted for input because no such input request is needed when what you need is already in the buffer. Thus it appears as if the user was simply ignored.

    A solution would be to place a getchar() before the fgets() call in such a case, however, a better solution might be to always use fgets() to read input from the user and parse it via sscanf() or some other method.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    25
    So anyway here's the code. I type 1 and it skips the input for the reverse string. please note this is a friend's code, i don't even do this subject but its always interesting to know why. I did a packet sniffer program in C last semester and came across similar errors but never knew EXACTLY why.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define strLength 120
    
    
    int main()
    {
    
        while (1)
        {
           menu();
        }
        return;              
    }
    
    
    int menu()
    {
         //variables 
         int nChoice;
         int n;
         int x;
         int fibCount;
         
    
         printf("\n\nChoose an Option.\n");
         printf("--------------------\n\n");
         printf("1. Reverse a String\n");
         printf("2. Fibonacci Sequence\n");
         printf("3. Get Statistics on a String\n");
         printf("4. Play Extended Tic-Tac-Toe\n");
         printf("5. Exit the Program\n\n");
         printf("Input Choice: ");
    
         scanf("&#37;d", &nChoice);
         
    
         switch(nChoice)
         {
    
                        case 1 : printf("\nYou chose Option 1: Reverse a String.\n");
                                 printf("---------------------------------------\n\n");
                                 RevString();
                                 break;
    
                        case 2 : printf("\nYou chose Option 2: Fibonacci Sequence.\n");
                                 printf("---------------------------------------\n\n");
                                 do
                                 {
                                     printf("How many numbers would you like displayed? (between 1 and 100): ");
                                     scanf("%d", &n);
                                 }while(n < 1 || n > 100);
    
                                 for(n = 0; n < fibCount  ; n++)
                                 {  
    
                                    x=Fib(n);
     
                                    printf("%d ", x);
                                 }
           
    
                                 break;
    
                        case 3 : printf("\nYou chose Option 3: Statistics of a String.\n");
                                 printf("-------------------------------------------\n\n");
                                 StatString();
                                 break;
    
                        case 5 : printf("\nThank You for using this Program.\n");
                                 printf("---------------------------------\n\n");
                                 exit(0);
                       
                        default: printf("Incorrect input.\n");
                                 printf("--------------\n\n");
                                 menu();
         }
    }
    
    int RevString()
    {
    
        char str[strLength];
        char revStr[strLength];
        int i = 0;
        int j = 0;
            
        printf("Enter the string to be reversed: \n\n");
        fgets(str, 120, stdin);
           
        for(i = strlen(str) - 1; i >= 0; i--)
        {
        
        
              revStr[j] = str[i];
        
        
              j++;
        }
        
        
        revStr[j] = '\0';
        
       
        printf("You inputted: %s\n", str);
        printf("Input reversed: %s\n", revStr);
       
        return;
    }
    Last edited by ahming; 07-27-2008 at 09:05 AM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Surprise. My explanation fits the given situation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM
  5. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 05:01 AM

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