I'm studying C language and I have a problem with scanf.
I want to implement integer input validation, but the way I do it, the terminal is full of garbage until I enter a valid value, I wanted to know if there is any way to delete entries that have not been validated.
If i press 'a', or any other letter. My terminal get one line per char
#include <stdio.h>
#include <windows.h>
float ler_num ();
void read_rate (float*, float*);
void calc_med (float*, float*, float*, float*);
int main(void)
{
float rate1 = 0;
float rate2 = 0;
float* prate1 = &rate1;
float* prate2 = &rate2;
float meds = 0;
float medp = 0;
float* pmeds = &meds;
float* pmedp = &medp;
read_rate(prate1, prate2);
return 0;
}
void read_rate (float* rate1, float* rate2){
printf("\nDigite o valor da 1 nota: ");
fseek(stdin, 0, SEEK_SET);
*rate1 = ler_num();
fflush(stdin);
printf("\nDigite o valor da 2 nota: ");
fseek(stdin, 0, SEEK_SET);
*rate2 = ler_num();
fflush(stdin);
}
float ler_num(){
float num = 0;
while (scanf("%f", &num) != 1){
static char temp [BUFSIZ];
fgets(temp, sizeof(temp), stdin);
}
return num;
}