0

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;
}
  • I see neither call of scanf in the presented code. – Vlad from Moscow Jun 29 '23 at 22:34
  • at ler_num(), in the last lines – Demétrios Guimarães Jun 29 '23 at 22:36
  • @Vlad: See the previous comment by OP. OP's code does contain `scanf`. – Andreas Wenzel Jun 29 '23 at 22:38
  • I reduced the code – Demétrios Guimarães Jun 29 '23 at 22:39
  • scanf is in function ler_num() – Demétrios Guimarães Jun 29 '23 at 22:41
  • If i press 'a', or any other letter. My terminal get one line per char – Demétrios Guimarães Jun 29 '23 at 22:42
  • Using `fflush(stdin)` is generally not recommended. See [this question](https://stackoverflow.com/questions/2979209/using-fflushstdin) for further information. Also, using `fseek` on the input stream is also generally not recommended, as it is not guaranteed what behavior this will have on different platforms. I suggest that you instead always make sure that you consume an entire line up to and including the newline character. – Andreas Wenzel Jun 29 '23 at 23:55
  • One way of discarding everything on the remainder of the line, including the newline character, is to call `fgets`, as you are doing in the function `ler_num`. Why are you not also doing that in the function `read_rate`? Why are you calling `fseek`/`fflush` instead? – Andreas Wenzel Jun 30 '23 at 00:28
  • What exactly do you mean with "garbage"? Do you mean the invalid input that the user typed? Or is the program printing nonsense characters? Are you asking how to delete what the user typed from the screen? If the user enters invalid input, do you want that input to get deleted and the text cursor on the screen to be moved back to the previous position? Is that what you are asking for? – Andreas Wenzel Jun 30 '23 at 01:24
  • If you want the ability to move the text cursor on the screen, then this is not possible in standard C. But there are platform-specific extensions which allow this. In that case, please specify which platform (operating system and compiler) you are using. – Andreas Wenzel Jun 30 '23 at 01:28

0 Answers0