Thread: Scanf/Fgets and input issues

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    1

    Post Input issues with scanf in simple program (please help!)

    Hello everyone.

    I was wondering why I have this really tedious problem where the terminal basically does not ask the user for input.

    The code down here is for a program that simply stores basic information about N workers, via a structure. As you can see from the picture I uploaded down here, there's no way I can insert a worker's surname.

    Could you help me find my mistake?

    Scanf/Fgets and input issues-schermata-2019-03-26-alle-12-27-23-png

    Code:
     
    
    
    1. #include <stdio.h>
    2. #define N 100
    3. int main (void)
    4. {
    5. typedef struct {
    6. char surname[40];
    7. int years_worked;
    8. float salary;
    9. } worker;
    10. worker ARRAY_WORKERS[N];
    11. for (int i = 0; i < N; i ++)
    12. {
    13. char ch;
    14. printf("Do you wish to enter data for a new worker ?\n");
    15. printf("0 to exit; 1 to continue.\n");
    16. //I am aware the program continues for any other key other than 0 :)
    17. if ( (ch = getchar()) == '0')
    18. break;
    19. printf("Enter a surname for the %d° worker: ", i + 1);
    20. fgets(ARRAY_WORKERS[i].surname, 40, stdin);
    21. printf("Enter the number of years worked: ");
    22. scanf("%d", &ARRAY_WORKERS[i].years_worked);
    23. printf("Enter the monthly salary (ex. 1350.55): ");
    24. scanf("%f", &ARRAY_WORKERS[i].salary);
    25. }
    26. return 0;
    27. }


    I tried using scanf instead of fgets, but the program works (does not) in a similar fashion. Basically I manage to insert the worker's surname, but the program then stops asking me if I want to stop. (Does not take input for ch).

    Scanf/Fgets and input issues-schermata-2019-03-26-alle-12-52-12-png

    Last edited by raffael_99; 03-26-2019 at 06:10 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The short answer is don't mix fgets with scanf.

    Code:
            char buff[BUFSIZ];
            printf("Do you wish to enter data for a new worker ?\n");
            printf("0 to exit; 1 to continue.\n");
            fgets(buff, BUSIZ, stdin);
            if ( buff[0] == '0')
                break;
            printf("Enter a surname for the %d° worker: ", i + 1);
            fgets(ARRAY_WORKERS[i].surname, 40, stdin);
            printf("Enter the number of years worked: ");
            fgets(buff, BUSIZ, stdin);
            sscanf(buff,"%d", &ARRAY_WORKERS[i].years_worked);
            fgets(buff, BUSIZ, stdin);
            printf("Enter the monthly salary (ex. 1350.55): ");
            sscanf(buff,"%f", &ARRAY_WORKERS[i].salary);
    Ideally, do this
    Code:
            if ( fgets(buff, BUSIZ, stdin) != NULL ) {
                    if ( sscanf(buff,"%d", &ARRAY_WORKERS[i].years_worked) == 1 ) {
    The respective else clauses being
    - error in conversion
    - EOF or error in reading from stdin
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which one is better, scanf or fgets
    By sunil9211 in forum C Programming
    Replies: 1
    Last Post: 05-02-2018, 12:01 PM
  2. fgets after scanf, skips over fgets?
    By willane1965 in forum C Programming
    Replies: 1
    Last Post: 08-17-2014, 11:13 PM
  3. fgets and scanf
    By kkk in forum C Programming
    Replies: 10
    Last Post: 07-29-2011, 10:11 AM
  4. scanf vs fgets?
    By Matus in forum C Programming
    Replies: 65
    Last Post: 11-17-2008, 04:02 PM
  5. what happens after 'fgets' and 'scanf'
    By the bassinvader in forum C Programming
    Replies: 4
    Last Post: 07-30-2006, 03:04 PM

Tags for this Thread