C 如果报表打印需要结果,如何制作?

C 如果报表打印需要结果,如何制作?,c,if-statement,C,If Statement,这段代码有一个问题。问题在于 if语句中的问题 if(all_digits(to_find) && strlen(to_find) == 13) 每当我输入13个字符但不是从文件中输入时,它就会给我一条else语句的消息,但是打印Found。你好,世界也是。尽管找到了。你好,世界在if语句中。它不应该被打印出来。如何使if语句正确工作 #include <stdio.h> #include <string.h> #include <ctype.h

这段代码有一个问题。问题在于

if语句中的问题

if(all_digits(to_find) && strlen(to_find) ==  13)
每当我输入13个字符但不是从文件中输入时,它就会给我一条else语句的消息,但是打印
Found。你好,世界也是。尽管找到了
。你好,世界在if语句中。它不应该被打印出来。如何使if语句正确工作

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int all_digits(char *s){
  for (; *s!=0; s++){
    if (!isdigit(*s)){
      return 0;
    }
  }
  return 1;
}

要在不级联的情况下运行多个测试,可以使用错误标志和一次性循环,如下所示:

int err = 0; /* Be optimistic! (0 indicates success) */

do {
   if (!test1-passed) {
     err = 1;
     break;
   }

   if (!test2-passed) {
     err = 2;
     break;
   }

   ...

   if (!testN-passed) {
     err = N;
     break;
   }

   printf("Success! All tests passed");
} while (0);

if (err) {
  printf("Test %d failed", err);
} 
应用于特定问题,代码可能如下所示

... /* definitions here */

int err = 0; /* Be optimistic! (0 indicates success) */

do {
  ... /* input here */

  do {
    if (!all_digits(to_find)) {
      err = 1;
      break;
    }

    if (strlen(to_find) != 13) {
      err = 2;
      break;
    }

    {
      err = 3 /* be pessimistic! */

      while(fgets(line, 200, fr)){
        /* word = strtok(line, "\n"); */ /* word is not needed. */
        strcpy(save, line); /* Introducing save here is not necessary, 
                               all following operation can be applied to line. */

        if (strstr(save, to_find)){
          char *wordone = strtok(save, ",");
          while (wordone != NULL){
            printf("Here are your details: %s\n", wordone);
            wordone = strtok(NULL, ",");
            err = 0; /* Phew, we are lucky! */
          }
        }
      }

      if (err) {
        break;
      }
    }

    printf("Success! All tests passed");
  } while (0);

  if (err) {
    printf("Test %d failed", err);
  } 
} while (err);

一般建议-将您的
所有数字
strlen
组合,这样它将一次遍历字符串。“每当我输入13个字符[…]时,它会给我一条else语句的消息…”我无法复制它。它工作正常。计算机正在执行您告诉它的操作。
fclose(fr)仅在满足if条件时调用。至少当
fr!=NULL(这实际上是一个缺少的检查)。我不理解“它给我一条else语句的消息”的含义。什么信息?你能告诉我们你得到的完整输出和一个导致该输出的输入字符串的例子吗?里面只有一个if语句,而看着我的例子,沉思并变得开悟…;)好的,我会冥想。谢谢你的启示!如果你要使用
goto
,请鼓起勇气拼写它
goto
:-P@zwol:如果您尝试使用goto级联此类构造,那么。。。不
... /* definitions here */

int err = 0; /* Be optimistic! (0 indicates success) */

do {
  ... /* input here */

  do {
    if (!all_digits(to_find)) {
      err = 1;
      break;
    }

    if (strlen(to_find) != 13) {
      err = 2;
      break;
    }

    {
      err = 3 /* be pessimistic! */

      while(fgets(line, 200, fr)){
        /* word = strtok(line, "\n"); */ /* word is not needed. */
        strcpy(save, line); /* Introducing save here is not necessary, 
                               all following operation can be applied to line. */

        if (strstr(save, to_find)){
          char *wordone = strtok(save, ",");
          while (wordone != NULL){
            printf("Here are your details: %s\n", wordone);
            wordone = strtok(NULL, ",");
            err = 0; /* Phew, we are lucky! */
          }
        }
      }

      if (err) {
        break;
      }
    }

    printf("Success! All tests passed");
  } while (0);

  if (err) {
    printf("Test %d failed", err);
  } 
} while (err);