【问题标题】:how to compare user input to a string for a while loop in c programming如何在c编程中将用户输入与字符串进行比较
【发布时间】:2015-05-27 02:08:30
【问题描述】:

我们正在尝试根据用户输入的内容执行 if 语句。例如,如果用户输入日元,那么我们希望执行 if 语句并计算到日元的转换。 在用户输入了多少加仑的气体后,我们就会计算出总成本。然后,我们想让用户选择他们想要将总成本转换成的货币。我们给了用户三个选择。如果用户输入的内容不是任何给定的选择,则允许用户再次重新输入选择。用户确实输入了 3 个选项之一,然后我们希望执行货币转换。

#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("Please enter the number of gallons of gasoline: ");
    scanf("%c", &input);

    while (!isdigit(input))
    {
        printf("\nYou need to enter a digit. Please enter the number of gallons of gasoline: ");
        scanf("%c", &input);
        }
    if (isdigit(input))
    {
        printf("\nThe users input was %c", input);
        gas_gallons = input - '0';
        printf("\nAs a float it is now %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("%c", &currency);
    printf("\nThe currency chosen is %c", currency);

    char Yen[100];
    char Euro[100];
    char Pound[100];

    while (strcmp(currency, Yen) != 0 || strcmp(currency, Euro) != 0 || strcmp(currency, Pound) != 0)
    {
        printf("\nYou need choose one of these currencies (Euro, Pound or Yen). Please enter one: ");
        scanf("%s", &currency);
    }
    if (strcmp(currency, Yen) == 1)
    {
        float yen_total_cost = cost_today_gallons*123.07;
        printf("\n%.2f gallons of gasoline costs a total average of %f US dollars today.", gas_gallons, yen_total_cost);
    }
    if (strcmp(currency, Euro) == 1)
    {
        float euro_total_cost = cost_today_gallons*0.92;
        printf("\n%.2f gallons of gasoline costs a total average of %f US dollars today.", gas_gallons, euro_total_cost);
    }
    if (strcmp(currency, Pound) == 1)
    {
        float pound_total_cost = cost_today_gallons*0.65;
        printf("\n%.2f gallons of gasoline costs a total average of %f US dollars today.", gas_gallons, pound_total_cost);
        }
    return 0;
}

【问题讨论】:

  • 尽可能简化你的生活,并提供一个数字菜单,让他们可以输入一个整数,你可以switch on。
  • 启用编译器警告,你会发现一些问题,也不清楚你想做什么,多次询问对你没有帮助,它可能会再次关闭。
  • 我的问题是,当用户输入问题“选择一种您想查看总成本(欧元、英镑或日元)的货币:”的答案时,我希望它接受输入并执行相应的代码。如果输入是欧元,那么我希望执行欧元的 if 语句。
  • 但是您并没有解释为什么没有发生这种情况,警告的事情,这真的很重要,您的代码有错误。
  • 你已经描述了你想要发生的事情。但是您没有描述实际发生的不正确的事情,并且您没有提出具体(甚至任何)问题。

标签: c string if-statement input compare


【解决方案1】:

我可以在您的代码中看到一些问题:

  1. 你应该使用scanf("%s", str)来读取一个字符串,见scanf()
  2. 另外,strcmp() 在其参数相等时返回 0

  3. 并且您的代码中的char[] Yen, Euro, Pound 未初始化。

【讨论】:

  • 当我被提示选择一种货币时,我输入了一种货币,它只是不断重复我需要选择一种货币,因此即使我输入了货币,它也不会执行任何 if 语句这是给定的选择之一。
  • @NicolRamirez 您是否正确初始化 char[] 日元、欧元、英镑?并检查最后3个strcmp(),strcmp()参数相等时返回0。如果还是不行,可以尝试打印货币来检查输入是否正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多