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++; }
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!
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...There, see, wasn't that hard now was it?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'
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.
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
%*1[/n] means get one newline, and ignore (*) it. The "r" is so you can test the return value of scanf when debugging:Code:int r; r=scanf("%27[a-zA-Z0-9_-]%*1[\n]", pn);
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
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!
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.
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
It's kind of customary to capitalize non-function preprocessor defines, it makes reading the code easier.