Re: how can we check to not enter the any string or char?



emre esirik(hacettepe computer science and engineering) wrote:

int n_mines;
printf("How many mines do you want in the minefield?");
scanf("%d", &n_mines);

scanf is a tricky function to use. If you do use it, you _must_ check
it's return value to ensure that it has succeeded.

If it fails it can, in certain situations, leave some input still in the
stdin's buffer, which will cause further calls to scanf to fail
repeatedly, until the stream is flushed.

while(n_mines>100 || n_mines<0)
{
printf("Please only enter between 1 and 100");
scanf("%d", &n_mines);
}

so I dont want to user can enter the string or char value,how can I
check it?

Frankly I advocate using fgets to input a line and strtoul or strtol to
convert to a numerical value. These functions provide more control and
better error reporting than either atoi or scanf/sscanf.

Also consider changing the above loop to a do/while one.


.