Я пытаюсь получить программу, чтобы позволить пользователю ввести валюту, в которую они хотят изменить общую стоимость. когда пользователь вводит неверный ввод, а затем предлагается ввести снова правильную валюту, когда они вводят правильную валюту, цикл while не прерывается. Он снова просит пользователя ввести правильную валюту.Пока цикл не работает для программирования c
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", ¤cy);
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", ¤cy);
}
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', было нормально – qrdl