Thread: need help with fgets

  1. #1
    Unregistered
    Guest

    need help with fgets

    well my fgets wont work...it goes directly to the next printf

    Code:
    case 2: 
    printf("Enter text:");
    fgets(text, sizeof(text), stdin);
    p = strchr( text, '\n' );
    if ( p != NULL ) *p = '\0';
    printf("\nblah blah blah");
    Sleep(3000);
    ....
    ....
    It dosnt let me enter any text...anyone knows why?

    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because your switch isn't hitting case 2?

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

  3. #3
    Unregistered
    Guest
    huh? what do you mean? here is what i get

    Code:
    2
    Enter text:
    blah blah blah
    i typed 2...so it went to case 2 and then it said Enter text: but it wont let me any time to type anything...it prints blah blah blah right away...

    thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You probably have a newline character left over in your input buffer then. Post more of your code.

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

  5. #5
    Unregistered
    Guest
    ok

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    #include <windows.h>
    #include <conio.h>
    
    
    int text1(void);
    int text2(void);
    int text3(void);
    int text4(void);
    int text5(void);
    
    int main (void)
    {
    char text[500];
    char *p;
    long start;
    long end;
    int ans;
    int display;
    
    printf("Please select one of the two following choice\n\n");
    printf("1) Show text\n");
    printf("2) Enter a text\n");
    scanf("%d", &ans);
    
    switch (ans)
    {
    case 1:
    printf("Which text do you want to display?\n");
    printf("1) Text 1\n");
    printf("2) Text 2\n");
    printf("3) Text 3\n");
    printf("4) text 4\n");
    printf("5) Text 5\n");
    scanf("%d", &display);
    
    switch (display)
    {
    case 1: //text1();
    break;
    case 2: //text2();
    break;
    case 3: //text3();
    break;
    case 4: //text4();
    break;
    case 5: //text5();
    default: printf("Invalid input\n");
    break;
    }
    
    case 2:
    printf("Enter text:");
    fgets(text, sizeof(text), stdin);
    p = strchr( text, '\n' );
    if ( p != NULL ) *p = '\0';
    printf("\nblah blah blah");
    Sleep(3000);
    ...
    ...

  6. #6
    Unregistered
    Guest
    sorry the code looks bad a little...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yeah, you're mixing scanf with fgets

    Do one of these two things

    Code:
    scanf("%d", &ans);
    while ( getchar() != '\n' );  /* remove excess chars */
    Or
    Code:
    fgets(text, sizeof(text), stdin);
    sscanf( text, "%d", &ans);

  8. #8
    Unregistered
    Guest
    ok thanks it works fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM