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?
Code:
- #include <stdio.h>
- #define N 100
- int main (void)
- {
- typedef struct {
- char surname[40];
- int years_worked;
- float salary;
- } worker;
- worker ARRAY_WORKERS[N];
- for (int i = 0; i < N; i ++)
- {
- char ch;
- printf("Do you wish to enter data for a new worker ?\n");
- printf("0 to exit; 1 to continue.\n");
- //I am aware the program continues for any other key other than 0 :)
- if ( (ch = getchar()) == '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: ");
- scanf("%d", &ARRAY_WORKERS[i].years_worked);
- printf("Enter the monthly salary (ex. 1350.55): ");
- scanf("%f", &ARRAY_WORKERS[i].salary);
- }
- return 0;
- }
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).