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