0

So. Im new to C programming. I've heard not to use fflush somewhere and would like to know any alternatives that are way more clean and useful that fflush for my inputs. Without fflush, fgets would not work properly.

This function is just to append a file, employees.txt with the new hire's Name and Occupation

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {

    char hireName[20];
    char hireRole[20];
    char hireSalary[20];
    float salaryF;
    char endProgram[10];

    printf("Ah.. its time again? Who did you hire this time? and whats their salary?\n");

    while (1) {
        /* Grabs Hire Name */
        printf("Name: ");
        fgets(hireName, 20, stdin);
        fflush(stdin);

        /* Grabs Hire Occupation */
        printf("Occupation: ");
        fgets(hireRole, 20, stdin);
        fflush(stdin);

        /* Grabs Hire's Salary */
        printf("Salary: ");
        fgets(hireSalary, 20, stdin);
        fflush(stdin);

        /* Wants to know if it should continue another hire (RESTARTS PROGRAM) */
        printf("Continue? Y/Yes N/No: ");
        fgets(endProgram, 5, stdin);

        /*
        * salaryF turns hireSalary from a char to a float type
        * removes breakline from variables (endProgram, hireName, hireRole)
        */
        salaryF = strtof(hireSalary, NULL);
        endProgram[strcspn(endProgram, "\n")] = 0;
        hireName[strcspn(hireName, "\n")] = 0;
        hireRole[strcspn(hireRole, "\n")] = 0;
        fflush(stdin);
        //printf("Demo - Name: %s | Occupation: %s | Salary: %f | endProgram: %s\n", hireName, hireRole, salaryF, endProgram);

        /* Opens employees.txt and appends the hire's name and the hire's occupation to the file!  */
        FILE * fEMPLOY = fopen("employees.txt", "a");
        fprintf(fEMPLOY, "\n%s, %s", hireName, hireRole);
        fclose(fEMPLOY);


        if (strcmp(endProgram, "N") == 0 || strcmp(endProgram, "n") == 0 || strcmp(endProgram, "no") == 0 || strcmp(endProgram, "No") == 0) {
            break;
        } else {
            continue;
        }

    }

    return 0;
}
  • 2
    `fflush(stdin)` is undefined behavior https://stackoverflow.com/questions/9122550/fflushstdin-function-does-not-work – mediocrevegetable1 Feb 24 '21 at 03:17
  • Microsoft used to define `fflush` for an input stream to flush received but unprocessed data. I think they may have dropped that, in which case you should not use it for that in current software. If a user enters a longer line than you expect, you can use `getchar()` to read characters (and ignore them) until a new-line character is read. – Eric Postpischil Feb 24 '21 at 03:19
  • 3
    Try to kick the habit of using undersized buffers for everything. 20 is absolutely microscopic! Use 255 or 1024 as a default. Declare this as a constant, or `#define` macro so you can use it *consistently* rather than typing the same number everywhere. – tadman Feb 24 '21 at 03:22
  • Why do you think your program needs to `fflush(stdin)`? What happens if you remove it? -- Oh, please [edit] your question with new information, don't add a comment. Take the [tour] and read "[ask]" to learn how to use this site. It is not a forum. – the busybee Feb 24 '21 at 07:39
  • In most cases a simple `getchar();` call could be used instead. – Lundin Feb 24 '21 at 11:16

0 Answers0