【问题标题】:Stopping an infinite while loop using scanf in c?在c中使用scanf停止无限循环?
【发布时间】:2015-03-15 06:48:52
【问题描述】:

我创建了一个程序,它接受由 cmets 分隔的 3 个数字,我将使用它来进行一些计算。它将接受这些数字的用户输入,我正在使用 scanf 来接受。

这是我目前所拥有的:

#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdbool.h>

int main(void)
{
    float a, b, c;

    bool continue_program = true;

    while (continue_program) {
        printf("Enter your coordinates: ");
        scanf("%f,%f,%f", &a,&b,&c);
        if(isdigit(a) && isdigit(b) && isdigit(c)){
            printf("Success!");
        } else {
            printf("Try again!");
        }
    }
}

示例输出:

Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!Enter your coordinates: Try again!

我知道其他人也遇到了同样的问题,并为这些问题提供了答案,但无法让他们的实现适用于此代码。

【问题讨论】:

    标签: c++ c while-loop scanf


    【解决方案1】:

    您没有在代码中更新continue_program 的值。因此它的值保持为真,因此是无限循环。您必须将其更新为假才能停止它。

    【讨论】:

      【解决方案2】:

      你错了scanf() 将返回匹配的参数数量,而你没有检查。

      另外,isdigit() 函数接受一个整数,如果传递的参数的 ascii 值对应一个数字,则返回非 0

      为了让你的程序在满足条件时停止,你应该在循环内改变continue_program的值,假设你想在scanf()没有读取3浮点数时停止,它将返回一个不同于3 的值,因此您可以将其设置为if 条件

      #include <stdio.h>
      #include <math.h>
      #include <ctype.h>
      #include <stdbool.h>
      
      int main(void)
      {
          float a, b, c;
      
          bool continue_program = true;
      
          while (continue_program) {
              printf("Enter your coordinates: ");
              if (scanf("%f,%f,%f", &a, &b, &c) == 3){
                  printf("Success!");
              } else {
                  continue_program = false;
                  printf("Try again!");
              }
          }
      }
      

      OP 在 cmets 之后建议使用此解决方案

      #include <stdio.h>
      #include <math.h>
      #include <ctype.h>
      #include <stdbool.h>
      #include <string.h>
      
      int main(void)
      {
          float a, b, c;
          char line[100];
      
          printf("Enter your coordinates: ");
          while (fgets(line, sizeof(line), stdin) != NULL) {
              size_t length;
      
              length = strlen(line);
              if (line[length - 1] == '\n')
                  line[length - 1] = 0;
              if (strcmp(line, "0,0,0") == 0)
                  break;
              if (sscanf(line, "%f,%f,%f", &a, &b, &c) == 3)
                  printf("\tSuccess!\n");
              else
                  printf("\tTry again!\n");
              printf("Enter your coordinates: ");
          }
      }
      

      【讨论】:

      • 好吧,知道它如何返回值有很大帮助。我如何继续向他们询问坐标?将布尔值设置为 false 将结束整个循环。
      • 这取决于你想什么时候结束循环?
      • @RandomMaker:请说明您希望程序何时停止。是在“成功”之后还是在“失败”之后?
      • 我不明白这里所有的反对票......无论如何我会为这个答案做一个+1。
      • @RandomMaker 我将发布一个我刚刚写的简单解决方案,检查一下。
      猜你喜欢
      • 2014-08-26
      • 1970-01-01
      • 2015-12-27
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多