1

How would I be able to clear the buffer if a character or more is entered in this block of code.

int x = 1;
float grade = 0.0;

do
{
    printf ("Enter a grade for quiz %d: ", x);
    scanf ("%f", grade);

    if (grade >= 1 && grade <= 10) break;

    printf ("Entry not valid. Please try again\n");

} while (1);

My instructor is insisting that we use fflush(stdin). This I know wont work, and I called him out on it. What other ways could I just to validate if a number is entered or not.

Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • you could simply read in using a getline approach, and parsing the string yourself to ensure it is numeric with only a single decimal point. After verification, you would then convert to an actual numeric type, then process however you want. – trumpetlicks Feb 07 '14 at 15:24
  • 1
    Why wouldn't fflush work? I use it and it is great! I guess you are using it the wrong way. –  Feb 07 '14 at 15:56
  • user689 how would you use fflush correctly? I've put in fflush(stdout) in many places, but my code continues to enter an infinite loop. – user3284399 Feb 09 '14 at 19:00
  • @user3284399 You should do `fflush(stdin)`, not `fflush(stdout)`. Also there is an error in your code, it should be `&grade` instead of `grade` in the `scanf()` call. – Piovezan Dec 19 '14 at 13:32

2 Answers2

1
void flush_stdin ()
{
    char c;
    do
    {
        c = get(stdin);
    }while(c != EOF && c != '\n');
}

I use this in my own code, basically, read every character in stdin until you encounter a sign that it is now empty.

I ll also urge to avoid scanf, but prefer using fread with sscanf, so you can limit the number of input character, wich avoid buffer overflow and other nasty things.

DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
  • 1
    Might choose a do-while instead so that the initial getchar call doesn't have to be made. – trumpetlicks Feb 07 '14 at 15:27
  • Thanks, I m trying to remember that piece of code that I c/p from project to project everytime, at least I did something functionnal at the first try – DrakaSAN Feb 07 '14 at 15:32
  • Totally hear you. I always need to look back at my code, and can rarely get it right on here first time :-) – trumpetlicks Feb 07 '14 at 15:36
  • DrakenSAN I did not know sscanf existed since I'm in my first year of my classes. Thank you sir. – user3284399 Feb 10 '14 at 14:28
  • @user3284399: Don t worry, we were all like you once. Think about reading the other function and library on the link I provided, all C and C++ standart library are described and explained, it help a lot when you are coding ; ). Also, think about accepting an answer by clicking the green mark at the left of the best answer to your question, it help maintaining the site and give rep point to you and the one who wote the answer. – DrakaSAN Feb 11 '14 at 13:08
0

Rather than attempting to flush the input buffer on a bad read, always read a whole line using fgets()/sscanf().

Example Read_long

do
{
    char buf[50];
    printf ("Enter a grade for quiz %d: ", x);
    if (fgets(buf, sizeof buf, stdin) == NULL) 
        Handle_EOForIOError();
    if (sscanf(buf, "%f", grade) == 1 && grade >= 1 && grade <= 10) 
        break;
    printf ("Entry not valid. Please try again\n");
} while (1);
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256