The purpose of this program is to count the digits in an alphanumeric input. However, I used a loop to not execute the program unless the input is alphanumeric.
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
int main(){
int input,isanum,digitcount;
printf("\nEnter a number: ");
scanf("%d",&input);
isanum=isalnum(input);
while(isanum==0){
printf("\nIncorrect input. Try again: ");
scanf("%d",&input);
isanum=isalnum(input);
}
digitcount=0;
while(input!=0){
input=input/10;
digitcount++;
}
printf("\nNumber of digits = %d",digitcount);
return 0;
}
The problem is with the loop. It keeps looping infinitely and ignores the scanf statement and I don't know why. Am I using isalnum()
incorrectly here?