0
printf("Enter an integer: ");
status = scanf("%d", &integer);

if (status == 0){
    do{
        printf("Please enter an integer: ");
        status = scanf("%d", &integer);
    }
    while (status == 0);
}

I'm trying to prevent a user from entering a data of type of character. However, after the prompt, "Please enter an integer: ", it doesn't wait for an input. Hence, it goes into an infinite loop whenever I enter a letter at the first prompt. How do I fix this? Any heeelp will be greatly appreciated!

Satellite Sage
  • 440
  • 1
  • 4
  • 13
  • 1
    You’ll need to read the non-digit somehow. The `”%d”` won’t read it ever. Or will see it, recognize it isn’t a digit and stop, leaving it for the next attempt. – Jonathan Leffler Aug 14 '18 at 00:14
  • @klutt, I also checked that already, but the error there was because scanf stores the carriage return (hitting enter after the prompt). I don't think it's applicable to this one because %d automatically skips white spaces. – Satellite Sage Aug 14 '18 at 00:16
  • Even if you fixed your loop, the code wouldn't detect a user entering something like `5x`. – bruceg Aug 14 '18 at 00:19

2 Answers2

0

You need clean the buffer first, you can use fflush(stdin); like this:

int integer, status=0;
if (status == 0)
{
    do
    {
        printf("\nPlease enter an integer: ");
        status = scanf("%d", &integer);
        fflush(stdin);
    }
    while (status == 0);
}

It's not in standard C using fflush(stdin) but you can clean the buffer in other ways.

You can build your own function to clean the buffer, like this:

void flushKeyBoard()
{
    int ch; //variable to read data into
    while((ch = getc(stdin)) != EOF && ch != '\n');
}

To clean the screen call this function:

void clrscr()
{
    system("@cls||clear");
}

Final code:

#include <stdio.h>

void clrscr()
{
    system("@cls||clear");
}
void flushKeyBoard()
{
    int ch; //variable to read data into
    while((ch = getc(stdin)) != EOF && ch != '\n');
}
int main()
{
    int integer, status=0;
    if (status == 0)
    {
        do
        {
            printf("\nPlease enter an integer: ");
            status = scanf("%d", &integer);
            flushKeyBoard();
            clrscr();
        }
        while (status==0);
    }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Fábio Morais
  • 272
  • 1
  • 9
  • Hey, it works. But it prints "Please enter an integer" twice after entering the first incorrect data. Do you have any idea how to fix it? – Satellite Sage Aug 14 '18 at 00:38
  • `do{ printf("Please enter an integer: "); status = scanf("%d", &integer); }` => `printf("Please enter an integer: "); do{ status = scanf("%d", &integer); }` – 0___________ Aug 14 '18 at 00:40
  • @LenovoGenovia I will edit my answer – Fábio Morais Aug 14 '18 at 00:42
  • Even though this works on many systems I think it is a very bad idea to teach people to use `fflush` on input streams. It is undefined behavior. – klutt Aug 14 '18 at 00:47
  • @klutt I did not say it was a good way, I write the flushKeyBoard() – Fábio Morais Aug 14 '18 at 00:49
  • Either you should remove that section completely or make it **abundantly** clear that it is potentially **very** dangerous. I would vote to remove it. Those who have a good use for that already knows. – klutt Aug 14 '18 at 00:52
  • @FábioMorais, I'm new to C, and this will probably be annoying, but... I'm getting an error saying "Function system should have a prototype." So I'm guessing I'm missing a header file? – Satellite Sage Aug 14 '18 at 00:53
  • 1
    @LenovoGenovia Just a friendly advice. Googling error messages is a thing programmers do every day. Get used to it. – klutt Aug 14 '18 at 00:54
  • @LenovoGenovia And another friendly advice. Stop using Turbo C. It's non-standard and was discontinued ages ago. – klutt Aug 14 '18 at 00:58
  • @klutt, I've heard that a lot, but our professor requires us to us Turbo C. – Satellite Sage Aug 14 '18 at 01:00
  • 1
    @LenovoGenovia You just need the #include , maybe it's the clrscr() function, search for other functions to clean the screen. [Example](https://stackoverflow.com/questions/2347770/how-do-you-clear-the-console-screen-in-c) – Fábio Morais Aug 14 '18 at 01:03
-2

your %integer, should be declared int. Like that:

int integer;

 printf("Please input an integer value: ");

 scanf("%d", &integer);