controlling input

how to i limit the input to 1-9 only and when i enter a-z or A-Z it will prevent the programming from converting the input?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define TIME 500
int main()
{
    int x=0,dec,rem,quo,dec1,msb=8,remArray[20];
    do
    {
     printf("\n\n\tPlease enter decimal value (1 - 999999): ");
     scanf("%d",&dec);
      if(dec>999999||dec<0)
      {
         printf("\tPlease enter value between 1 - 999999.");
         _sleep(TIME);
      }
      else if(dec<1000000||dec>-1)
      {
          for(x=0;x<msb;x++)
          {
             quo=dec/16;
             rem=dec%16;
             printf("\t16 ) %d  \t- %d\n",dec,rem);
             dec=quo;
             printf("\t   --------\n");_sleep(TIME);
          }
      }
    }while(dec>999999||dec<0);
}
Last edited on
bump
If input fails, scanf will return EOF. You should look in a reference for things like this. When that happens, you'll probably need to clear the input stream. I'm not well familiar with c libraries, so I can only suggest while( getchar() != '\n' );.

By the way, the condition on line 17 is wrong. There is no number which wouldn't satisfy it. There is no need for a condition there at all. "else" makes sure that no number in the wrong range gets to that point.
i have to do a message that do "printf("\tPlease enter value between 1 - 999999.");" whenever either 'a' to 'z' or 'A' to 'Z' is input, then repeat to ask for input again...
If input fails, scanf will return EOF
When an integer is expected and a letter is written, it's a failure. Is there something you don't understand in my post?
Topic archived. No new replies allowed.