Thread: gets function

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    42

    gets function

    I am haing a problem with the loop in my code.
    It works fine until the second iteration through the loop.
    When I make the loop into the code:

    printf("Please enter the first word\n");

    gets(str1);
    printf("%s a\n", str1);

    count1 = strlen(str1);

    printf("Please enter the second word\n");

    gets(str2);
    printf("%s a\n", str2);
    count2 = strlen(str2);
    The code appears to pass right by the first printf() and gets(str1)
    and goes directly to str2 printf() and gets.

    What is causing this problem?

    The first time into the loop it works correctly.

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    I guess it's because there is a '\n' left in the stdin stream after reading in the first string.
    You just have to flush stdin with this or a similar function before gets(str2) is called:
    Code:
    void tidy (FILE *in)
    {
        int c;
        while( (c = fgetc(in)) != EOF && c != '\n');
    }
    You can not write the following:

    fflush(stdin);

    This is undefined by the ANSI C99 Standard!

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    I had added printf() to do some debugging. This is what I have without the debug printf().
    here is the whole code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define str_length 100
    void main()
    {

    char str1[100];
    char str2[100];
    int count1;
    int count2;
    char more ; /* variable to continue with the loop y||Y */




    do
    {

    printf("Please enter the first word:");

    gets(str1);


    count1 = strlen(str1);

    printf("Please enter the second word:");

    gets(str2);

    count2 = strlen(str2);


    /* concatenate str2 to end of str1 */
    if(count1 == count2 ||count1 < count2 )
    {
    printf(" %s %s and \n", str1,str2);

    printf(" %s %s\n",strrev(str1), str2);

    }
    else
    /* concatenate str1 to end of str2 */
    {
    printf(" %s %s and \n", str2,str1);

    printf(" %s %s \n",strrev(str2),str1);

    }

    printf(" Would you like to continue? y or n:\n");
    scanf("%c", &more);


    }while(more == 'y' || more == 'Y');




    }

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    What is causing the problem? few things on your code.
    ->void main()
    The main function return's an integer, so change it to int main() and return an value at the end of main().

    ->gets(str2);
    Don't use gets(), search on the board about it, use instead fgets()
    http://man.he.net/man3/fgets

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    It still doesnt work.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define str_length 100
    int main()
    {

    char str1[100];
    char str2[100];
    int count1;
    int count2;
    char more ; /* variable to continue with the loop y||Y */




    do
    {

    printf("Please enter the first word\n");

    fgets(str1);
    printf("%s a\n", str1);

    count1 = strlen(str1);

    printf("Please enter the second word\n");

    fgets(str2);
    printf("%s a\n", str2);
    count2 = strlen(str2);


    /* concatenate str2 to end of str1 */
    if(count1 == count2 ||count1 < count2 )
    {
    printf(" %s %s and \n", str1,str2);

    printf(" %s %s\n",strrev(str1), str2);

    }
    else
    /* concatenate str1 to end of str2 */
    {
    printf(" %s %s and \n", str2,str1);

    printf(" %s %s \n",strrev(str2),str1);

    }

    printf(" Would you like to continue? y1 or n:\n");
    scanf("%c", &more);


    }while(more == 'y' || more == 'Y');

    return 0;


    }

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    It's because you use the scanf function. This function has the nasty habbit to leave newline characters in your input buffer.
    You can try to flush your input buffer with the following line:
    Code:
    scanf("%c", &more); 
    while(getchar() != '\n') ; /* flush input buffer */
    For more information: FAQ

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    That was it! It worked. Thanks. The information on the scanf leaving a newline character is interesting. Does this always occur in a scanf() or is it intermitent?

  9. #9
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    >> Does this always occur in a scanf() or is it intermitent?

    For normal use with the scanf function: Yes

    This is one of the reasons I always use the fgets function to read from input and for example the sscanf function scan the buffer.

    There are however some tricks you can use with the scanf function so prevent the function from leaving the newline character in the input buffer.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Cool. Thanks for the help, the info. and the links.

  11. #11
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You're welcome.

    b.t.w. take a look at Salem's advice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM