繁体   English   中英

Do...While... 继续循环,如果发生其他情况。 C

[英]Do...While... keeps on looping, if else case occurs. C

只要输入没有错误,程序就会正常运行。 例如,如果我输入“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();

输出 1错误输出

使用gets是危险的,因为万一读取行超过99 个字符,函数会以未定义的行为写出数组。 使用fgets没有这个问题,但读一行,实际上你想读一个单词,你不关心它之前或之后的空格,在isspace的意义上(所以也包括换行符,返回,制表符, ...),一种实用的方法是使用scanf ,在当前情况下scanf(" %99s", day) ,注意 '%' 之前的空格以绕过可能的初始空格。

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

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

要使用printf按原样打印字符串是昂贵的,并且假设它不包含 '%' 等,否则行为未定义,如果您想要尾随的换行符 else fputs (检查字符串是否齐平),请使用puts

所以例如:

#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;
}

编译和执行:

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:~ $ 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM