【问题标题】:C Programming Logic Error?C编程逻辑错误?
【发布时间】:2011-02-15 05:57:15
【问题描述】:

以下代码编译良好,但不允许用户选择是否再次运行程序。给用户答案后,程序自动终止。我将主代码放在一个“do while”循环中,如果我愿意,我也可以转换不止一次。我尝试在命令行(Mac 和 Ubuntu 机器)和 XCode 中运行该程序,结果完全相同。任何帮助将不胜感激。

  • C 初学者

附:在运行 Snow Leopard 的 MacBookPro 上编译。


#include <stdio.h>
#include <stdlib.h>

int main(void)
{

        char anotherIteration = 'Y';

        do
        {
                const float Centimeter = 2.54f;
                float inches = 0.0f;
                float result = 0.0f;

                // user prompt
                printf("\nEnter inches: ");
                scanf("%f", &inches);

                if (inches < 0) {
                        printf("\nTry again. Enter a positive number.\n");
                        break;
                } else {
                        // calculate result
                        result = inches * Centimeter;
                }

                printf("%0.2f inches is %0.2f centimeters.\n", inches, result);

                // flush input
                fflush(stdin);

                // user prompt
                printf("\nWould you like to run the program again? (Y/N): ");
                scanf("%c", &anotherIteration);

                if ((anotherIteration != 'Y') || (anotherIteration != 'N'))
                {
                        printf("\nEnter a Y or a N.");
                        break;
                }

        } while(toupper(anotherIteration == 'Y'));

        printf("Program terminated.\n");

        return 0;
}

【问题讨论】:

  • 下次请格式化您的代码。

标签: c


【解决方案1】:

你可能是说……

} while(toupper(anotherIteration) == 'Y');

既然要转换字符,那么将其与'Y'比较。

【讨论】:

    【解决方案2】:

    条件

    if ((anotherIteration != 'Y') || (anotherIteration != 'N')) 
    

    始终为真,因此无论用户输入如何,您的程序都会终止。

    此外,您将break 放入代码中的几乎每个if 中,以处理错误输入。 break 将终止循环和程序。这是一个相当奇怪的逻辑:要求用户再试一次,然后立即终止程序,而不给用户真正再试一次的机会。为什么要终止程序而不是让用户重新输入?

    【讨论】:

      【解决方案3】:

      嗯,

      while(toupper(anotherIteration == 'Y'));
      

      看来你是想说

      while(toupper(anotherIteration) == 'Y');
      

      .. 但可能还有其他问题。

      【讨论】:

      • 虽然该代码很奇怪,但我不知道这会如何产生他看到的结果。 anotherIteration=='Y' 应该返回 true (1),并且 toupper(1) 应该是 1。导致一个真正的循环。相反,他看到了循环结束。
      • @abelenky:是的,我就是这么想的,它永远不应该终止它的外观。因此对“其他问题”的抨击=)
      【解决方案4】:

      这行得通。

      /* convert inches to centimeters */
      
      #include <stdio.h>
      #include <ctype.h>
      
      int main(void)
      {
      
       char anotherIteration = 'Y';
      
       do
       {
        const float Centimeter = 2.54f;
        float inches = 0.0f;
        float result = 0.0f;
      
        // user prompt
        printf("\nEnter inches: ");
        scanf("%f", &inches);
      
        if (inches < 0)
        {
         printf("\nTry again. Enter a positive number.\n");
         break;
        }
        else
      
         // calculate result
         result = inches * Centimeter;
      
        printf("%0.2f inches is %0.2f centimeters.\n", inches, result);
      
        // flush input
        fflush(stdin);
      
        // user prompt
        printf("\nWould you like to run the program again? (Y/N): ");
        scanf("%c", &anotherIteration);
      
       } while(toupper(anotherIteration) != 'N');
      
       printf("Program terminated.\n");
      
       return 0;
      }
      

      【讨论】:

        【解决方案5】:

        这里有两个大错误。第一个是你在你的 问题:

        } while(toupper(anotherIteration == 'Y'));
        

        anotherIteration == 'Y' 将返回 1 或 0,之后两者都等于 0 正在通过toupper

        你想要的是:

        } while(toupper(anotherIteration) == 'Y');
        

        另一个错误在这里:

        printf("\nWould you like to run the program again? (Y/N): ");
        scanf("%c", &anotherIteration);
        
        if ((anotherIteration != 'Y') || (anotherIteration != 'N'))
        {
            printf("\nEnter a Y or a N.");
            break; // This breaks out of hte main program loop!
        }
        

        您真正想做的是再次询问用户是否输入错误, 像这样:

        do
        {
            printf("\nWould you like to run the program again? (Y/N): ");
            scanf("%c", &anotherIteration);
            if ((anotherIteration != 'Y') && (anotherIteration != 'N'))
                printf("\nEnter a Y or a N.");
        } while ((anotherIteration != 'Y') && (anotherIteration != 'N'));
        

        【讨论】:

        • toupper(1) 被记录为返回 1:“如果不可能进行此类转换,则返回的值是 c 不变。”为什么你认为 toupper 会返回 0?
        • @abelenky:因为如果它不返回 0,那么程序就不会终止。
        • 谢谢比利先生。感谢您的宝贵时间。
        【解决方案6】:

        你有一些错误,但既然你正在学习,你可能应该找出它们。您对此程序的具体问题的答案是,您可能希望将fpurge() 用于stdin,而不是fflush()

        【讨论】:

        • 谢谢卡尔先生。我想我最好多关注标准库。我去看看 fpurge 和 fflush 的区别。
        • @mbpluvr64, man fflush 应该告诉你你需要知道的一切。
        • @mbpluvr64:由于您在输入之前打印了一个提示,您确实想在printf 调用之后调用fflush(stdout)。此外,如果您将scanf 替换为fgetssscanf 的组合,则根本不需要“刷新”stdin(这通常是个好主意)。
        【解决方案7】:

        一个错误:

        while(toupper(anotherIteration == 'Y'))
        

        应该是

        while(toupper(anotherIteration) == 'Y')
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多