【问题标题】:Exiting a loop without break statement?退出没有break语句的循环?
【发布时间】:2014-05-14 14:00:49
【问题描述】:

对于家庭作业,我需要提示用户输入单词,直到他们输入 20 个单词或输入单词“完成”。现在我有一个带有 while 循环的函数,它扫描输入的每个单词,但是当用户键入“完成”时,循环并没有结束。

功能:

int wordentry(int row, int col, char a[20][100])

{

int i = 0;/* Counter */


    printf("Enter 20 words you would like hidden in the puzzle, or type 'done' when finished:\n");

    /* While the user hasn't entered 20 words, or the user hasn't entered the word 'done' the prompt to enter words keeps looping */

    while(i < 20)
    {
      scanf("%c",a[i]);
      if(strcmp(a[i],"done") == 0)
      {
        return(i);
      }
      i++;

    }/* Ends while loop */



return(i);

}/* Ends function */

变量row 是用户确定的变量,它是每个单词要插入的单词搜索谜题的行长度,变量col 是用户确定的每列长度。数组a 是将每个单词存储为字符串的位置。返回变量i 用于跟踪用户输入的单词数。另外,除非有switch 语句,否则我不能使用任何break 语句。我很沮丧,因为我不知道如何正确地做到这一点。有人可以帮忙吗?

【问题讨论】:

  • 建议if (scanf("%99s",a[i]) != 1) Handle_Error_Somehow();

标签: c arrays loops multidimensional-array


【解决方案1】:

要在没有break 语句的情况下退出循环,您需要满足保持循环运行的条件。所以当你有以下情况时:

while (i < 20)

i 设置为20 或更大将阻止循环再次执行。不同之处在于它将持续到当前运行结束,而不是立即退出。

【讨论】:

    【解决方案2】:

    scanf 只读取一个字符,这就是%c 所表示的。所以比较是将'd'与不相等的“done”进行比较。使用%s,这样您就可以输入整个单词,然后继续调试您的问题。

      scanf("%s",a[i]);
    
      if(strcmp(a[i],"done") == 0)
      ... 
    

    【讨论】:

      【解决方案3】:

      我认为 if 语句中的 strcmp 总是返回 false。我对C编程语言没有很好的经验,但是尝试放这样的东西

      ...
      scanf("%s",&a[0]);
      if(strcmp(a[i],"done") == 0)
      ...
      

      (不要忘记包含 string.h)

      【讨论】:

        【解决方案4】:

        避免冗余和硬编码值,任何改变控制流的条件都值得明确说明,因此避免 goto、continue 和 break(除了在 switch 语句中)并且字符很便宜,所以给你的变量一些有意义的名称:

        #define MAXWORDS 20
        #define MAXCHARS 99
        
        int wordEntry(int row, int col, char words[MAXWORDS][MAXCHARS+1])
        {
        
            int numEntered = 0;
            int userDone = 0;
            char *doneWord = "done";
        
            printf("Enter %d words you would like hidden in the puzzle, or type '%s' when finished:\n", MAXWORDS, doneWord);
        
            /* While the user hasn't entered max words, or the user hasn't entered the word 'done' the prompt to enter words keeps looping */
        
            while( !userDone && (numEntered < MAXWORDS) )
            {
                scanf("%s",words[numEntered]);
                if (strcmp(words[numEntered],doneWord) == 0)
                {
                    userDone = 1;
                }
                else
                {
                    numEntered++;
                }
        
            }/* Ends while loop */
        
            return numEntered;
        
        }/* Ends function */
        

        在决定使用 scanf() 之前,还要阅读 http://c-faq.com/stdio/scanfprobs.html

        【讨论】:

        • 很好的改进!尼特:doneWord 可能是char const * const
        猜你喜欢
        • 1970-01-01
        • 2017-03-31
        • 1970-01-01
        • 1970-01-01
        • 2013-11-17
        • 2016-02-08
        • 1970-01-01
        • 1970-01-01
        • 2019-12-27
        相关资源
        最近更新 更多