I need to determine whether or not a user input is numeric. I need to loop over each characters and check if all are digits, if not printf()
Error message, or else Successful. I used a for loop to loop over each characters and an if
statement with isdigit()
function to check the digit. But my problem is I am printing for each characters. For example, if I enter 22A
, I get 2 Successful messages for 2
and 2
, and then I get Error message for A
. But I need just print the error message since 22A
is not entirely numeric.
int n = strlen(argv[1]);
for (int i=0; i<n; i++){
if (isdigit((int)argv[1][i]) == 0){
printf("Error\n");
return 1;
}
else{
printf("Success\n");
printf("%i\n", atoi(argv[1]));
}
}