Thread: fgets in a function

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    fgets in a function

    Code:
    void create_entry()
    {
    
    fgets(a.fname, 51, stdin);
    printf("%s", a.fname);
    stop();
    }
    When this function is run it skips the fgets and goes straight onto the stop() function.

    But using the same three lines in int main() it works. How can i slove this?
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  2. #2
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    The fgets() itself looks fine, are you using scanf anywhere in your program?
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    Yes

    Yes I am a lot
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  4. #4
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    Now, there's many people with more knowledge of this than me but, from what I've learnt/been told scanf() is bad and it leaves the input stream in a terrible mess, after using scanf put this line of code in:

    Code:
    while ( getchar() != '\n' );
    This cleans up the mess left by scanf, seems to work for me...
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM