Reputation: 25
I am new to C programming and I want to check all my array elements are integer except the first element.
I wrote the following code, but the loop never stops once I insert wrong input.
bool validate_int(char input[]){
fgets(input,10, stdin);
for(int i = 1; i < strlen(input); ++i) {
if(!isdigit(input[i])){
i = 1;
fgets(input,10, stdin);
}
else{
}
}
return true;
}
Upvotes: 0
Views: 207
Reputation: 2304
Check this:
int a_Length = 10;
char input[a_Length];
fgets(input,a_Length, stdin);
for(int i = 1; i < strlen(input); ++i) {
if(!isdigit(input[i])&& input[i]!='\n'){
i = 0;
printf("Again try: ");
if (input[a_Length - 1 ]!='\n')
getchar();
fgets(input,10, stdin);
}
}
Upvotes: -1
Reputation: 1177
Here is another approach I would take, which is must cleaner IMO :
This is your validation function :
bool customValidation(char *string)
{
int len = strlen(string);
if (!isalpha(string[0]) || (len > 1 && !isdigit(string[1])))
return false;
for (int i = 1; i < len && string[i] != '\n'; ++i)
if (!isdigit(string[i]))
return false;
return true;
}
This is how you would use it :
char input[10];
do
{
fgets(input, 10, stdin);
} while (!customValidation(input));
Obviously you should rename customValidation() to something more significant.
Upvotes: 2
Reputation: 20244
Your code has some small problems. Here is an better way to do it (untested):
bool validate_int(char input[]) /* Bad function name; See @Filipe's comment */
{
for(;;) /* Infinite loop */
{
if(fgets(input, 10, stdin) == NULL) /* If fgets failed */
{
puts("fgets failed");
return false;
}
int i, len = strlen(input);
if(len > 0 && input[len - 1] == '\n') /* If there is a newline character at the end of input */
input[--len] = '\0'; /* Replace the '\n' with '\0' and decrement len */
if(!isalpha(input[0])) /* If the first character of input is not an alphabet */
continue; /* Loop again */
if(len == 1) /* There is no number */
continue;
for(i = 1; i < len; ++i)
{
if(!isdigit(input[i])) /* If not a digit */
continue; /* Loop again */
}
break; /* Get out of the loop */
}
return true;
}
A more better way would be to seperate input and validation into two seperate functions (untested):
bool getInput(char input[])
{
if(fgets(input, 10, stdin) == NULL) /* If fgets failed */
{
puts("fgets failed");
return false;
}
int len = strlen(input);
if(len > 0 && input[len - 1] == '\n') /* If there is a newline character at the end of input */
input[--len] = '\0'; /* Replace the '\n' with '\0' and decrement len */
return true;
}
bool validate(char input[])
{
if(!isalpha(input[0])) /* If the first character of input is not an alphabet */
return false;
int i, len = strlen(input);
if(len == 1) /* There is no number after the character */
return false;
for(i = 1; i < len; ++i)
{
if(!isdigit(input[i])) /* If not a digit */
return false;
}
return true;
}
and in the calling function (again, untested),
char input[10];
if(getInput(input))
{
if(validate(input))
{
puts("Input is in correct format");
}
else
{
puts("Input is in wrong format");
}
}
else
{
puts("Failed to get input");
}
Upvotes: 2
Reputation: 40145
try this
bool validate_int(char input[]){
bool valid;
do{
valid = false;
fgets(input,10, stdin);
for(int i = 1; input[i] && input[i] != '\n'; ++i) {
if(!isdigit(input[i])){
valid = false;
break;
} else {
valid = true;
}
}
}while(!valid);
return true;
}
Upvotes: 1