gets() command help?

This is a discussion on gets() command help? within the C Programming forums, part of the General Programming Boards category; Any idea why this fragment of code will result in the subroutine not completing? Whenever the sub "find" is run, ...

  1. #1
    Unregistered
    Guest

    gets() command help?

    Any idea why this fragment of code will result in the subroutine not completing? Whenever the sub "find" is run, the printf statment executes, but the user cannot enter an input because the routine ends.

    find(void)
    {

    char name_search[10];
    int i;

    printf("Enter name2:");

    gets(name_search);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The compiler knows you are using gets, which is a horrible function, and to save your potential damage, it self terminates. It would rather die than execute such a function.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    You are better off using fgets with the stdin stream. If you need help implementing that, then just say the word.

    --Garfield
    1978 Silver Anniversary Corvette

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    fgets( mystring, strlen(myString), stdin );

    where stdin is the associated "stream".
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    here is my implementation of a safe gets()
    Code:
    char* newgets(char* buffer,int num)
    {
    int i;
    fgets(buffer,num,stdin);
    i=strlen(buffer)-1;
    if (buffer[i]=='\n') buffer[i]='\0';
    return buffer;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> safe gets() <<

    That's an oxy-moron (sp?)

    --Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

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