做…而。。。如果发生其他情况,则继续循环。C

做…而。。。如果发生其他情况,则继续循环。C,c,loops,do-while,C,Loops,Do While,只要输入没有错误,程序就会正常运行。例如,如果我键入“sunday”,那么d的值将设置为1,但是如果我键入一些错误输入,比如“adfasdf”,并且在程序循环之后,它将继续循环 这是我菜单驱动程序的一部分 int d,error=0; do{ char day[100]; printf("Enter Day of the Week: ");

只要输入没有错误,程序就会正常运行。例如,如果我键入“sunday”,那么d的值将设置为1,但是如果我键入一些错误输入,比如“adfasdf”,并且在程序循环之后,它将继续循环

这是我菜单驱动程序的一部分

            int d,error=0;

            do{
                    char day[100];
                    printf("Enter Day of the Week: ");
                    getchar(); //as gets may accept \n
                    gets(day);
                    strlwr(day);
                    if (strcmp(day,"sunday")==0){
                       d=1;
                       error=1;
                    }
                    else if (strcmp(day,"monday")==0){
                        d=2;
                        error=1;}
                    else if (strcmp(day,"tuesday")==0){
                        d=3;
                        error=1;}
                    else if (strcmp(day,"wednesday")==0){
                        d=4;
                        error=1;}
                    else if (strcmp(day,"thursday")==0){
                        d=5;
                        error=1;}
                    else if (strcmp(day,"friday")==0){
                        d=6;
                        error=1;}
                    else if (strcmp(day,"saturday")==0){
                        d=7;
                        error=1;}
                    else{
                        printf("Invalid Entry!");
                        error=0; //So the program will loop till correct input is placed.
                    }

            }while(error==0);
            printf("%d",d);
            getch();

使用gets是危险的,因为如果读取行超过99个字符,函数将以未定义的行为从数组中写入。使用fgets没有问题,但是如果一个人读一行,事实上你想读一个单词,你不关心它前面或后面的空格,在isspace的意义上(也包括换行符、回车符、制表符等等),一种实用的方法是使用scanf,在当前的情况下是
scanf(“%99s”,day)
,请注意“%”之前的空格以绕过可能的初始空格

另一个问题是您没有检查gets(或getchar)的结果,因此在EOF情况下,您的程序循环没有结束。所以使用scanf检查它是否返回1

正如在备注中所说,error不是命名变量的好选择,事实上您不需要它,您可以在需要时退出for use break,或者在变量为0时用0初始化d并循环

要使用printf按原样打印字符串,成本很高,并且假设它不包含“%”等,否则行为未定义,如果要使用尾随换行符else fputs,请使用put(检查字符串是否齐平)

例如:

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

/* I do not have strlwr, I define it */
void strlwr(char * s)
{
  while (*s) {
    *s = tolower(*s);
     s += 1;
  }
}

int main()
{
  int d = 0;
  
  do {
    char day[100];
    
    printf("Enter Day of the Week: ");
    if (scanf(" %99s", day) != 1) {
      puts("read error");
      d = -1; /* or any other action at least exiting the for */
    }
    else {
      strlwr(day);
      
      if (strcmp(day,"sunday")==0)
        d=1;
      else if (strcmp(day,"monday")==0)
        d=2;
      else if (strcmp(day,"tuesday")==0)
        d=3;
      else if (strcmp(day,"wednesday")==0)
        d=4;
      else if (strcmp(day,"thursday")==0)
        d=5;
      else if (strcmp(day,"friday")==0)
        d=6;
      else if (strcmp(day,"saturday")==0)
        d=7;
      else 
        puts("Invalid Entry!");
    }
  } while (d == 0);
  
  if (d != -1)
    printf("%d\n",d);
  
  return 0;
}

为什么将错误设置为1表示有效输入,0表示无效输入?这似乎是倒退。那是做什么用的?向您解释欢迎使用SO!我们真的很讨厌文字截图。复制文本并将其直接粘贴到问题中,而不是屏幕截图。我想循环程序,直到正确的输入被放置。您等待直到有答案,然后如果您有权限,您可以向上投票。此外,您还可以选择最有用的一个,并在一段时间后接受它(不需要特权)。但这里没有答案。。。。你可以在评论中询问回答者(@user3121023),为什么他们没有创建一个答案,使之成为有用的问答对。
pi@raspberrypi:~ $ gcc -Wall c.c
pi@raspberrypi:~ $ ./a.out
Enter Day of the Week: today
Invalid Entry!
Enter Day of the Week:     yesterday
Invalid Entry!
Enter Day of the Week:    Monday
2
pi@raspberrypi:~ $ echo birthday | ./a.out
Enter Day of the Week: Invalid Entry!
Enter Day of the Week: read error
pi@raspberrypi:~ $