Kevin Javier
Kevin Javier

Reputation: 23

Error check if user enters a letter or number

I'm error checking for letters. If a letter is entered then its suppose to print out error and exit. If its a number then it's suppose to run the statement (which I didn't put in the code since its irrelevant at the moment) under the if. When I enter a number it should run the if statement but when I enter a letter or a number it goes to the else statement.

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


int main()
{
  int x;
  printf("Enter up to 10 positive integer ending with EOF:\n");

while((scanf("%d",&x)) != EOF && x < 100){


if( isdigit(x) ){
//statement
}

else{
printf("error, wrong input\n");
return 0;
}

}


if(x >= 100)
printf("error, wrong input\n");


return 0;
}

Upvotes: 0

Views: 2617

Answers (1)

BobRun
BobRun

Reputation: 754

You want isdigit to check a char , please change scanf to :

while((scanf("%c",&x)) != EOF && x < 100){  // yes x is an int, but here you want a char

Upvotes: 1

Related Questions