the presented above code is supposed to work
first i check if it starts with a number then i check if the returned char is "," then i check if i get number after it.
then i check if its the end '\0'
why its not working?
the presented above code is supposed to work
first i check if it starts with a number then i check if the returned char is "," then i check if i get number after it.
then i check if its the end '\0'
why its not working?
Am I wrong in thinking that user inputted text from stdin will not be null terminated. I am not really sure but maybe that is the problem?
One of the disadvantages of being a 22 year old RPG programmer is having to repeatedly explain to your friends that you don't make videogames for a living.
scanf() will *not* be terminated with anything, fgets() will be if room in the buffer, allows. iirc
Perhaps that's why Laserlight was suggesting getchar(), to pull the \n off the keyboard buffer. I have to say I haven't followed all of this drama.
where is the mistake
Code:#include <stdio.h> int is_valid(const char *input); const char *match_number(const char *input); int main() { int i; int ch; int input[40]; printf("enter string\n"); for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i) { input[i] = ch; } input[i] = '\0'; if (is_valid(input)) { printf("%s is valid.\n", input); } else { printf("%s is invalid.\n", input); } return 0; } /* Match one or more consecutive digits and return a pointer to the first char * that is not a digit. */ const char *match_number(const char *input) { while((*input<='9')&&(*input>='0')){ input++; //increasing the address by1 } return input; } int is_valid(const char *input) { if (((*input)<'0')||((*input)>'9')){ return 0; } input=match_number(input); if (*input==','){ if (((*input)<'0')||((*input)>'9')){ return 0; } input=match_number(input+1); if (*input=='\0'){ return 1; } } else { return 0; } }
You still need to learn to read code as it is written, not as you wish it to be.Code:if (*input==','){ /*Ten guesses as to what input is right here, first nine don't count */ /*Ten guesses as to whether the next if-statement will be true, first nine don't count */ if (((*input)<'0')||((*input)>'9')){ return 0; } input=match_number(input+1); if (*input=='\0'){ return 1; }
i changed it so it will check *(input+1)
still bot working
Code:#include <stdio.h> int is_valid(const char *input); const char *match_number(const char *input); int main() { int i; int ch; int input[40]; printf("enter string\n"); for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i) { input[i] = ch; } input[i] = '\0'; if (is_valid(input)) { printf("%s is valid.\n", input); } else { printf("%s is invalid.\n", input); } return 0; } /* Match one or more consecutive digits and return a pointer to the first char * that is not a digit. */ const char *match_number(const char *input) { while((*input<='9')&&(*input>='0')){ input++; //increasing the address by1 } return input; } int is_valid(const char *input) { if (((*input)<'0')||((*input)>'9')){ return 0; } input=match_number(input); if (*input==','){ if (((*(input+1))<'0')||((*(input+1))>'9')){ return 0; } input=match_number(input+1); if (*input=='\0'){ return 1; } } else { return 0; } }
Of course it doesn't work.
(Hint: input is an array of chars, not an array of ints.)Code:$ ./numbers enter string 2,1 2,1 is valid.
i am not using it as an int array
in every line its being used as a char array
I changed that
but "2,1" is valid
but this input "2,1 " should be invalid
in the end it checks if it equals '\0'
why its saying "2,1 " is valid??
Code:#include <stdio.h> int is_valid(const char *input); const char *match_number(const char *input); int main() { int i; int ch; char input[40]; printf("enter string\n"); for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i) { input[i] = ch; } input[i] = '\0'; if (is_valid(input)) { printf("%s is valid.\n", input); } else { printf("%s is invalid.\n", input); } return 0; } /* Match one or more consecutive digits and return a pointer to the first char * that is not a digit. */ const char *match_number(const char *input) { while((*input<='9')&&(*input>='0')){ input++; //increasing the address by1 } return input; } int is_valid(const char *input) { if (((*input)<'0')||((*input)>'9')){ return 0; } input=match_number(input); if (*input==','){ if (((*(input+1))<'0')||((*(input+1))>'9')){ return 0; } input=match_number(input+1); if (*input=='\0'){ return 1; } } else { return 0; } }
So I've been assuming you've been reading the messages from your compiler, but that's probably an unwise assumption. So the compiler says this:
because there is one way to get "no answer" out of your is_valid function, namely when you have spaces (or other characters) at the end of the input. So remember: every path must have a return value -- find the if with no else, and fix it.Code:$ gcc -Wall -Wextra -o numbers numbers.c numbers.c: In function ‘is_valid’: numbers.c:66: warning: control reaches end of non-void function
thanks i forgot to mention the else case in '\0' if