I can't seem to understand the concept of fflush()
function in C. Could someone explain it in simpler terms cause I cannot seem to grasp it and what it does in this code:
int main() {
loadContactList();
while (1) {
printf("\n");
printMenu();
int choice;
scanf(" %d", &choice);
fflush(stdin);
printf("\n");
if (choice == 1) {
// addContact();
} else if (choice == 2) {
} else if (choice == 3) {
} else if (choice == 4) {
query();
} else if (choice == 5) {
while (1) {
printf("choose the sorting mode:\n \n");
printf("1. Sort by last name, first name then number\n");
printf("2. Sort by date\n");
printf("Enter -1 to return to the main menu\n");
int x;
scanf("%d", &x);
if (x == 1) {
sortByLFN();
printContactList();
break;
} else if (x == 2) {
sortByDate();
printContactList();
break;
} else if (x == -1) {
break;
}
}
} else if (choice == 6) {
//saveContact();
} else if (choice == 7) {
quitPhoneBook();
} else {
printf("You entered an invalid option \n");
}
}
return 0;
}
the code is supposed to be for a phone book program and we where told to use fflush
but it wasn't explained in class.