Thread: What is wrong with my code?

  1. #16
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    here...
    Code:
    while((strcmp(pn,cmd3) !=  0) && (searchn < 2)){    	
    	printf("\n pn= \"%s\"\n",pn);    
    	fflush(stdout);	
    	printf("search for\n");	
    	scanf("%27[a-zA-Z0-9_-]", pn);	
    	searchn++;	
    	}
    Arduino rocks!

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, I'm aware of where the scanf line is. I wanted you to actually read and learn from what I've been saying to you. But I see that's far too much to ask of you...
    Code:
    #include <stdio.h>
    #include <string.h>
    #define c ".c"
    #define cmd1 "electropulse"
    #define cmd2 "weight"
    #define cmd3 "exit"
    
    /* eeew globals */
    int var[100];
    int var2[100];
    int searchn = 0;
    int lookn = 0;
    char pn[100];
    
    FILE *f;
    
    int main() {
        printf("Project name: ");
        scanf("%27[a-zA-Z0-9_-]", pn);
        strcat(pn,c);
    
        while(pn != cmd3 && searchn<2){    	
            printf("search for\n");	
            scanf("%27[a-zA-Z0-9_-]", pn);	
            if( strcmp( pn, cmd1 ) == 0 ){
                printf("cmd1\n");
                var[0]=1;    	
            }
            if( strcmp( pn, cmd2) == 0 ){
                printf("cmd2\n");
                var[1]=1;	
            }
            searchn++;	
            printf("pn = \'%s\'\n", pn );
        }
    
        return 0;
    }
    
    
    /* output: */
    Project name: search for
    pn = 'foo.c'
    search for
    pn = 'foo.c'
    
    
    There, see, wasn't that hard now was it?

    Now if we quickly look at the output, we see that like I said, you've got a newline stuck in your buffer, because you never take it out after the first scanf call.


    Quzah.
    Last edited by quzah; 10-10-2009 at 07:01 AM.
    Hope is the first step on the road to disappointment.

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by quzah View Post
    Now if we quickly look at the output, we see that like I said, you've got a newline stuck in your buffer, because you never take it out after the first scanf call.
    Yeah. scanf() has something to read, '\n', but it is not in the category [a-zA-Z0-9_-], so it fails right away.

    To get rid of the newline at the end of your input, you could do this
    Code:
    int r;
    r=scanf("%27[a-zA-Z0-9_-]%*1[\n]", pn);
    %*1[/n] means get one newline, and ignore (*) it. The "r" is so you can test the return value of scanf when debugging:
    Code:
    printf("\npn=\"%s\"\tr=%d\n"),pn,r); fflush(stdout);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #19
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    OK, I just used "%s" instead of "%27[a-zA-Z0-9_-]"...
    Sorry quzah, don't get upset so easy, i didn't notice that newline!
    Arduino rocks!

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not upset. You just need to learn how to debug and ask smart questions. It'll save you a lot of head aches in the long run. Just remember, every time you hit enter, unless you're using a function that specifically pulls it out, it's going to still be there.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by yann View Post
    OK, I just used "%s" instead of "%27[a-zA-Z0-9_-]"...
    Sorry quzah, don't get upset so easy, i didn't notice that newline!
    There's a problem with that: "exit\n" will not match "exit" when you want to compare strings. Etc. Get it? That's why it is better to throw the newline away -- not keep it in the string.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #22
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    It's kind of customary to capitalize non-function preprocessor defines, it makes reading the code easier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM