1

I'm using this piece of code to read users input and check if it is a number or not.But sincerly it just works for numbers and letters. I want it to work with every char. For example "!?%". I have already tried to change the "isalnum" by "isascii" but that does not work.

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

int main ()
  {

    int a;
    int b = 1;
    char c ;

     do
     { 
       printf("Please type in a number: ");

       if (scanf("%d", &a) == 0)
       {
         printf("Your input is not correct\n");
         do 
         {
           c = getchar();
         }
         while (isalnum(c));  
         ungetc(c, stdin);    
       }
       else
       { 
         printf("Thank you! ");
         b--;
       }

     }
     while(b != 0); 

     getchar();
     getchar();

     return 0;
  }
5
  • 2
    Could you fix your indentation, please? Commented Nov 13, 2010 at 11:59
  • I don't understand your question. What characters do you want the test to reject? Commented Nov 13, 2010 at 12:00
  • I want to reject all characters except numbers. Commented Nov 13, 2010 at 12:06
  • 2
    Are these numbers? -34, 4.5, 00000000000000000000000000003, 9.87654321, -23E-34, 0x42, 0b101010101, ... Commented Nov 13, 2010 at 12:08
  • No just simple numbers like 1, 2, 3, 4 and so on. The problem is that characters like !?% are not correctly rejected like letters. Commented Nov 14, 2010 at 20:20

2 Answers 2

2

Unless you have specific requirements, you should use fgets and sscanf

while (1) {
    char buf[1000];
    printf("Please input a number: ");
    fflush(stdout);
    if (!fgets(buf, sizeof buf, stdin)) assert(0 && "error in fgets. shouldn't have hapenned ..."):
    /* if enter pending, remove all pending input characters */
    if (buf[strlen(buf) - 1] != '\n') {
        char tmpbuf[1000];
        do {
            if (!fgets(tmpbuf, sizeof tmpbuf, stdin)) assert(0 && "error in fgets. shouldn't have hapenned ...");
        } while (buf[strlen(tmpbuf) - 1] != '\n');
    }
    if (sscanf(buf, "%d", &a) == 1) break; /* for sufficiently limited definition of  "numbers" */
    printf("That was not a number. Try again\n");
}
Sign up to request clarification or add additional context in comments.

Comments

2

A correct way in strictly C89 with clearing input buffer, checking overflow looks like:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int readLong(long *l)
{
  char *e,in[20];
  fgets( in,20,stdin );
  if(!strchr(in,'\n')) while( getchar()!='\n' );
  else *strchr(in,'\n')=0;
  errno=0;
  *l=strtol(in,&e,10);
  return *in&&!*e&&!errno;
}

int main()
{
  long l;
  if( readLong(&l) )
    printf("long-input was OK, long = %ld",l);
  else
    puts("error on long-input");
  return 0;
}

3 Comments

+1 but you should increase the size of in. It is possible that long is 64 bits and -9223372036854775808 (20 chars including '-') is valid but not accepted by your function.
Sorry but this i too complicated. I just want a simple solution. Is there anything i could use instead of isalnum(), i tried isascii() but this doesn't work for me.
Its too difficult for you to call a function? Its not to difficult for you to handle inputbuffer yourself everytime?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.