【问题标题】:While loop not working for c programmingWhile循环不适用于c编程
【发布时间】:2015-08-09 17:21:06
【问题描述】:

我正在尝试让程序允许用户输入他们想要将总成本更改为的货币。当用户确实输入了错误的输入,然后被提示再次输入正确的货币时,当他们输入正确的货币时,while循环不会中断。它不断要求用户再次输入正确的货币。

Please enter the number of gallons of gasoline: 7

7.0 gallons of gasoline produces..
7.0 gallons of gasoline requires.. 
7.0 gallons of gasoline costs..

Choose a currency you want to see your total cost in (Euro, Pound, or Yen): eurooo

You need choose one of these currencies (Euro, Pound, or Yen). Please enter one: Euro

You need choose one of these currencies (Euro, Pound, or Yen). Please enter one: Euro

You need choose one of these currencies (Euro, Pound, or Yen). Please enter one: 

代码:

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

float gas_gallons;
float cost_today_gallons;
int main()
{
    char input;
    printf("\nPlease enter the number of gallons of gasoline: ");
    scanf(" %c", &input);
    getchar();

    while (!isdigit(input))
    {
        printf("\nYou need to enter a digit. Please enter the number of gallons of gasoline: ");
        scanf("%c", &input);
        }

    if (isdigit(input))
    {
        gas_gallons = input - '0';

        float carbon_dioxide_pounds = gas_gallons * 19.64;
        printf("\n%.2f gallons of gasoline produces approximately %f pounds of carbon dioxide.", gas_gallons, carbon_dioxide_pounds );

        float barrels_crude_oil = gas_gallons/19.0;
        printf("\n%.2f gallons of gasoline requires %f barrels of crude oil.", gas_gallons, barrels_crude_oil);

        cost_today_gallons = gas_gallons*2.738;
        printf("\n%.2f gallons of gasoline costs a total average of %f US dollars today.", gas_gallons, cost_today_gallons);
        }

    char currency[100];
    printf("\nChoose a currency you want to see your total cost in (Euro, Pound, or Yen): ");
    scanf("%s", &currency);
    getchar();

    char *str1 = "Yen";
    char *str2 = "Euro";
    char *str3 = "Pound";

    while ((strcmp(currency, str1) != 0) || (strcmp(currency, str2) != 0) || (strcmp(currency, str3) != 0))
    {
        printf("\nYou need choose one of these currencies (Euro, Pound or Yen). Please enter one: ");
        scanf("%s", &currency);
    }

    if ((strcmp(currency, str1) == 0))
    {
        float yen_total_cost = cost_today_gallons*123.07;
        printf("\n%.2f gallons of gasoline costs a total average of %f Japenese Yens today.", gas_gallons, yen_total_cost);
    }
    if (strcmp(currency, str2) == 0)
    {
        float euro_total_cost = cost_today_gallons*0.92;
        printf("\n%.2f gallons of gasoline costs a total average of %f Euros today.", gas_gallons, euro_total_cost);
    }
    if (strcmp(currency, str3) == 0)
    {
        float pound_total_cost = cost_today_gallons*0.65;
        printf("\n%.2f gallons of gasoline costs a total average of %f British Pounds today.", gas_gallons, pound_total_cost);
    }


    return 0;
}

【问题讨论】:

  • 上次我检查while 没问题

标签: c loops while-loop conditional-statements break


【解决方案1】:

在这一行 while ((strcmp(currency, str1) != 0) || (strcmp(currency, str2) != 0) || (strcmp(currency, str3) != 0))

你告诉你的代码循环直到 currency 等于 str1 str2 str3 这永远不会发生。

您需要将其更改为 while ((strcmp(currency, str1) != 0) &amp;&amp; (strcmp(currency, str2) != 0) &amp;&amp; (strcmp(currency, str3) != 0))

【讨论】:

    【解决方案2】:

    scanf 还在末尾添加了\n。您需要将其砍掉,或将其添加到您要比较的字符串中。或者,如果您不关心有人输入“European money”而不是“Euro”,您可以使用 strncmp() 而不是 strcmp()。

    请注意,如果有人输入超过 98 个字符,您的代码很容易出现缓冲区溢出(添加 \n\0 后,您将位于字节 101)。为确保这不会导致问题,您应该使用 'scanf("%99s")' 代替,这将切断第 99 个字符之后的字符串(或者,使用不同的输入函数)。

    【讨论】:

      【解决方案3】:

      当您应该使用 AND 时,您在 while 语句中使用了 OR 运算符。无论选择哪种有效货币都将失败 2 个条件,因此您会陷入循环。

      【讨论】:

      • 非常感谢!!它现在可以工作了,我明白为什么它无法打破循环。
      • 没问题,多年来多次看到这个逻辑错误,我也被它绊倒了:-)
      【解决方案4】:

      有几个问题。

      这个条件是错误的

      while ((strcmp(currency, str1) != 0) || (strcmp(currency, str2) != 0) || strcmp(currency, str3) != 0))
      

      替换为:

      while ((strcmp(currency, str1) != 0) && (strcmp(currency, str2) != 0) && strcmp(currency, str3) != 0))
      

      作为练习,我让你找出原因。

      您对scanf 的使用过于复杂,您可能想要这样:

      float gas_gallons;
      float cost_today_gallons;
      int main()
      {
          printf("\nPlease enter the number of gallons of gasoline: ");
          scanf("%f", &gas_gallons);
      
          float carbon_dioxide_pounds = gas_gallons * 19.64;
          printf("\n%.2f gallons of gasoline produces approximately %f pounds of carbon dioxide.", gas_gallons, carbon_dioxide_pounds );
      
          float barrels_crude_oil = gas_gallons/19.0;
          printf("\n%.2f gallons of gasoline requires %f barrels of crude oil.", gas_gallons, barrels_crude_oil);
      
          cost_today_gallons = gas_gallons*2.738;
          printf("\n%.2f gallons of gasoline costs a total average of %f US dollars today.", gas_gallons, cost_today_gallons);
      
      
          char currency[100];
          printf("\nChoose a currency you want to see your total cost in (Euro, Pound, or Yen): ");
          scanf("%s", currency);
      
          char *str1 = "Yen";
          char *str2 = "Euro";
          char *str3 = "Pound";
      
          while ((strcmp(currency, str1) != 0) && (strcmp(currency, str2) != 0) && (strcmp(currency, str3) != 0))
          {
              printf("\nYou need choose one of these currencies (Euro, Pound or Yen). Please enter one: ");
              scanf("%s", &currency);
          }
      
          if ((strcmp(currency, str1) == 0))
          {
              float yen_total_cost = cost_today_gallons*123.07;
              printf("\n%.2f gallons of gasoline costs a total average of %f Japenese Yens today.", gas_gallons, yen_total_cost);
          }
          if (strcmp(currency, str2) == 0)
          {
              float euro_total_cost = cost_today_gallons*0.92;
              printf("\n%.2f gallons of gasoline costs a total average of %f Euros today.", gas_gallons, euro_total_cost);
          }
          if (strcmp(currency, str3) == 0)
          {
              float pound_total_cost = cost_today_gallons*0.65;
              printf("\n%.2f gallons of gasoline costs a total average of %f British Pounds today.", gas_gallons, pound_total_cost);
          }
      
      
          return 0;
      }
      

      奖励:变量名 str1str3 的选择很差。为什么不选择名称为strEurostrYenstrPound

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-25
        相关资源
        最近更新 更多