Thread: how to fix this pattern code..

  1. #106
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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?

  2. #107
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    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.

  3. #108
    Registered User
    Join Date
    Sep 2006
    Posts
    8,866
    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.

  4. #109
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by shoutatchickens View Post
    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?
    User input, that is read in with %s, will be null terminated. User input, that is read in character by character, will be newline-terminated (which is what the above code assumes).

  5. #110
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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;
      }
    }

  6. #111
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    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;
          }
    You still need to learn to read code as it is written, not as you wish it to be.

  7. #112
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by tabstop View Post
    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;
          }
    You still need to learn to read code as it is written, not as you wish it to be.
    this is a part of a code i posted before
    whats the mistake in it?

  8. #113
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by transgalactic2 View Post
    this is a part of a code i posted before
    whats the mistake in it?
    You've still got ten guesses left. (Although of course there's no need to guess, if you've got even .01 of a clue.)

  9. #114
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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;
      }
    }

  10. #115
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Of course it doesn't work.
    Code:
    $ ./numbers
    enter string
    2,1
    2,1 is valid.
    (Hint: input is an array of chars, not an array of ints.)

  11. #116
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am not using it as an int array

    in every line its being used as a char array

  12. #117
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by transgalactic2 View Post
    i am not using it as an int array

    in every line its being used as a char array
    Quote Originally Posted by transgalactic2 View Post
    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];
    /* et cetera */
    Want to try that again?

  13. #118
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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;
      }
    }

  14. #119
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    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:
    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
    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.

  15. #120
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks i forgot to mention the else case in '\0' if

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM