jeudi 23 juillet 2020

infinite loop in C when the user enters a character instead of a number

I have a problem, I want to make the user enter a digit from 1.00 to 10.00 and if he enters a digit outside of that range I will display that he was wrong and try again. I think I have done well but the problem is if the user enters a letter the loop repeats at infinity, I would appreciate your help. Thank you. :)

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

int main()
{
    float a;

    do
    {
        printf("Insert number between 1.00 and 10.00:\n");
        scanf("%f", &a);

        if (a < 1.00 || a > 10.0)
        {
            printf("Insert a correct number.\n");
        }
    } while (a < 1.00 || a > 10.00);

    system("pause");
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire