本文介绍了strcmp()如果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这个代码不应该有效吗?如果不是,为什么不应该呢? #include< stdio.h> int main(void) { char yesno [10]; char * yes =" yes" ;; char * no =" no"; printf(" Select:yes or no\ n); fgets(yesno ,sizeof(yesno),stdin); if(strcmp(yes,yesno)== 0) { printf(" yes \ n"); } if(strcmp(no,yesno)== 0) { b $ b printf(no\\\); } 返回0; } 解决方案 ei ** ***@grex.cyberspace.org (Eirik)写道: 这段代码不应该有效吗?如果没有,为什么不应该呢? 这取决于你如何定义工作。如果程序收到 字符y,e和s,紧接着是文件结尾 条件,在标准输入上,它打印是。然后是换行符 字符。如果收到''n''和'o'',再接着是文件结尾 条件,则打印出否。接着是换行符。如果那是 该程序应该做什么,那么它的工作原理。 如果`fgets''读取整行,那么程序无法打印要么是'yes \ n'要求 或no\\\,因为如果有一个,那么`fgets''会存储最终的换行符。 /> #include< stdio.h> 由于程序使用`strcmp'',我建议您还包括 < string.h> ;. int main(void) {yes yesno [10]; char * yes =" yes" ;; 由于指针本身未被修改,并且内存指向 (字符串文字)无法修改,我建议你同时使用 常数: const char * const yes =" yes" ;; char * no =" no" ;; 同样。 printf(" Select:yes or no\ n); 这里也可以使用更简单的`puts''函数。 fgets(yesno,sizeof(yesno),stdin); 如上所述,`fgets''存储最终换行符,如果在文件结尾或发生错误之前有一个。另外,你可能想要检查 的返回值:如果发生错误或文件结束时它是一个空指针 在至少一个字符出现之前发生阅读。 if(strcmp(yes,yesno)== 0) { printf(" yes \ n"); 再次,'puts''也是可能的。 } if(strcmp(no,yesno)== 0) 因为如果前一个条件已经确定,这个条件不可能是真的,你可以通过写作来避免不必要的测试 else if(strcmp(no,yesno)== 0) { printf(" no\\\);; 再次,'puts''也是可能的。 } 返回0; } Martin fgets包含用户要验证的第一个CR字符 他的回答是'为什么strcmp返回1而不是0. 为了避免你可以这样做: if(strncmp(是,yesno,3)== 0) { printf(" yes \ n"); } - UnVéritablelui,code en fortran。 " Eirik" < EI ***** @ grex.cyberspace.org> écritdansle message de news: 76 ************************* @ posting.google.com ... 难道这段代码不行吗?如果不是,为什么不应该呢? #include< stdio.h> int main(无效) { char yesno [10]; char * yes =" yes" ;; char * no =" no" ;; printf("选择:是或否\ n"); fgets(yesno,sizeof(yesno),stdin); if(strcmp(yes,yesno)== 0) { printf(" yes \ n"); } if(strcmp(no,yesno)== 0) { printf(" ; no\\\; } 返回0; } ei*****@grex.cyberspace.org (Eirik)写道: 这段代码不应该有效吗?如果不是,为什么不应该呢? #include< stdio.h> int main(无效) { char yesno [10]; char * yes =" yes" ;; char * no =" no" ;; printf(" Select:yes or no\\\ ;); fgets(yesno,sizeof(yesno),stdin); 如果输入是然后yesno将包含yes \ n, if(strcmp(yes,yesno)== 0) { printf(" yes \ n ); 所以你不会来这里。 Shouldn''t this code work? If not, why shouldn''t it? #include <stdio.h> int main(void) { char yesno[10];char *yes = "yes";char *no = "no"; printf("Select: yes or no\n");fgets(yesno, sizeof(yesno), stdin); if(strcmp(yes, yesno) == 0){printf("yes\n");}if(strcmp(no, yesno) == 0){printf("no\n");} return 0; } 解决方案 ei*****@grex.cyberspace.org (Eirik) writes: Shouldn''t this code work? If not, why shouldn''t it?That depends on how you define "work". If the program receives thecharacters ''y'', ''e'', and ''s'', immediately followed by an end-of-filecondition, on standard input, it prints "yes" followed by a newlinecharacter. If it receives ''n'' and ''o'', again followed by an end-of-filecondition, it prints "no" followed by a newline character. If that iswhat the program is supposed to do, then it works. If `fgets'' reads a full line, then the program cannot print either "yes\n"or "no\n", because `fgets'' stores the final newline character if there isone. #include <stdio.h>Since the program uses `strcmp'', I recommend that you also include<string.h>. int main(void) { char yesno[10]; char *yes = "yes";Since the pointer itself is not modified, and the memory pointed to(a string literal) cannot be modified, I recommend that you make bothconstant: const char *const yes = "yes"; char *no = "no";Likewise. printf("Select: yes or no\n");The simpler `puts'' function would also have been possible here. fgets(yesno, sizeof(yesno), stdin);As explained above, `fgets'' stores the final newline character if there isone before end-of-file or an error occur. Also, you might want to checkthe return value: it is a null pointer if an error occurs or end-of-fileoccurs before at least one character has been read. if(strcmp(yes, yesno) == 0) { printf("yes\n");Again, `puts'' would also have been possible. } if(strcmp(no, yesno) == 0)Since this condition cannot be true if the previous condition has alreadybeen true, you could avoid an unnecesary test by writing else if (strcmp (no, yesno) == 0) { printf("no\n");Again, `puts'' would also have been possible. } return 0; }Martinfgets include the first CR caracter that the user type to validatehis answer that''s why strcmp returns 1 and not 0. To avoid that you can do : if(strncmp(yes, yesno, 3) == 0){printf("yes\n");} --Un Véritable lui, code en fortran. "Eirik" <ei*****@grex.cyberspace.org> a écrit dans le message de news: 76*************************@posting.google.com... Shouldn''t this code work? If not, why shouldn''t it? #include <stdio.h> int main(void) { char yesno[10]; char *yes = "yes"; char *no = "no"; printf("Select: yes or no\n"); fgets(yesno, sizeof(yesno), stdin); if(strcmp(yes, yesno) == 0) { printf("yes\n"); } if(strcmp(no, yesno) == 0) { printf("no\n"); } return 0; }ei*****@grex.cyberspace.org (Eirik) writes: Shouldn''t this code work? If not, why shouldn''t it? #include <stdio.h> int main(void) { char yesno[10]; char *yes = "yes"; char *no = "no"; printf("Select: yes or no\n"); fgets(yesno, sizeof(yesno), stdin);If you enter "yes" then yesno will contain "yes\n", if(strcmp(yes, yesno) == 0) { printf("yes\n"); so you won''t get here. 这篇关于strcmp()如果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-22 18:34