所以,我正在为我的 ENGR200 类(class)编写一个工程类型的程序,但我完全陷入困境。我以前从未接触过编程语言,但我在过去 3 小时内搜索了网络,测试了循环的每种变体,但似乎没有任何效果。
我想要程序做的是
- 接收用户输入
- 测试是否在可接受的范围内
- 如果没有,我希望它发送错误消息
- 提示输入新条目
- 如果它确实符合可接受的范围,我希望它继续到其余的范围。
我当前的代码如下:
/*******************************************************************************
AUTHOR SECTION
ENGR 200.xx DATE: mm/dd/yyyy
PROGRAM: # Author:
********************************************************************************
PROGRAM DESCRIPTION
This program will allow the user to enter the temperature in fahrenheit
and relative humidity in percent. Then ask the user wether he/she will be in
direct sun. Next, if an unacceptable temp or relative humidiy is entered, an
error message should be printed. If the error check is passed, the program
will then calculate the heat index and humidex. The program will then print
these values temp in degrees fahrenheit, the temperature in degree celsius,
the relative humidity, the heat index, and the humidex. Based on the heat
index value the program will prin and appropriate warning below the output.
If heat index is below 80F print "No health concerns expected."
DESCRIPTION OF VARIABLES
NAME TYPE DESCRIPTION
heat_index double Calculated value for heat index
humi_index double Calculated value for humidex
humi_f double Calculated variable used in humi_index
temp int User input for degrees fahrenheit
temp_c double Calculated value for conversion f --> C
relat_humid int User input of relative humidity in % [0-100]
humi_pow double power value for humi_f calculation
direct_sun char User input for direct sun contact or not
heat_index_y double heat_index + 15
humi_index_y double humi_index + 9
-----------------------------------------------------------------------------
FORMULAS
humi_pow = (((7.5)*(temp_c))/((237.7)+(temp_c)))
heat_index =
(-42.379)+(2.04901523*temp)+
(10.14333127*relat_humid)-
(0.22475541*temp*relat_humid)-
((6.83783*(pow(10,-3)))*(pow(temp),2))-
((5.481717*(pow(10,-2)))*(pow(relat_humid,2)))+
((1.22874)*(pow(10,-3))*(pow(temp,2))*(relat_humid))+
((8.5282)*(pow(10,-4))*(temp)*(pow(relat_humid,2)))-
((1.99*(pow(10,-6))*(pow(temp,2))*(pow(relat_humid,2)))
humi_f =
((6.112)*(pow(10,humi_pow)))*((relat_humid)/100.0)
humi_index =
(temp_c+((5.0/9.0)*(humi_f-10)))
*******************************************************************************/
/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
/* Main function */
int main()
{
/* Declare variables */
char direct_sun;
double heat_index, humi_index, temp_c, humi_pow, humi_f, humi_index_y,
heat_index_y, temp, relat_humid;
/* Print headings */
printf("********************************************");
printf("\n APPARENT HEAT CALCULATION");
/* Input values */
printf("\n\nEnter temperature in degrees Fahrenheit: ");
scanf("%d",&temp);
/* Check valididty of temp */
while(temp < 67)
{
printf("You must input a value greater than 67 degrees fahrenheit.");
printf("\nEnter temperature in degrees Fahrenheit: ");
scanf("%d",&temp);
if(temp >= 67)
{
break;
}
}
printf("Enter relative humidity (56% as 56) : ");
scanf("%d",&relat_humid);
/* Check validity of relat_humid */
while(relat_humid <= 0 || relat_humid >= 100)
{
printf("You must input a value between 0 and 100.");
printf("\nEnter relative humidity (56% as 56) :");
scanf("%d",&relat_humid);
if(relat_humid <= 100 && relat_humid >= 0)
{
break;
}
}
printf("Working in direct sun (y/n)? : ");
scanf("%c",&direct_sun);
/* Compute */
temp_c = ((5.0/9.0)*(temp-32));
humi_pow = (((7.5)*(temp_c))/((237.7)+(temp_c)));
humi_f = ((6.112)*(pow(10,humi_pow)))*((relat_humid)/100.0);
humi_index = (temp_c+((5.0/9.0)*(humi_f-10)));
heat_index =
(-42.379)+(2.04901523*temp)+
(10.14333127*relat_humid)-
(0.22475541*temp*relat_humid)-
((6.83783*(pow(10,-3)))*(pow(temp,2)))-
((5.481717*(pow(10,-2)))*(pow(relat_humid,2)))+
((1.22874)*(pow(10,-3))*(pow(temp,2))*(relat_humid))+
((8.5282)*(pow(10,-4))*(temp)*(pow(relat_humid,2)))-
(1.99*(pow(10,-6))*(pow(temp,2))*(pow(relat_humid,2)));
heat_index_y = heat_index + 15;
humi_index_y = humi_index + 9;
/* Print output values */
printf("\n\nRESULTS");
printf("\nTemperature (degrees F):");
printf("\nTemperature (degrees C):");
printf("\nRelative Humidity:");
printf("\n\nHeat index:");
printf("Humidex:");
printf("WARNING: IF STATEMENT REQUIRED");
printf("\n********************************************\n\n\n");
/* Exit the program */
return 0;
}
/*********************************************************************/
最佳答案
以下内容可能可以满足您的需要:
#include <stdio.h>
#define CUTOFF (67)
int main() {
int temperature = 0;
int scanf_result = 0;
do {
/* Input values */
printf("Enter temperature in degrees Fahrenheit: ");
scanf_result = scanf("%d", &temperature);
fpurge(stdin); // clear out excess or non-numeric input
/* Check validity of temperature */
if (scanf_result == 0 || temperature < CUTOFF) {
fprintf(stderr, "You must input a value greater than %d degrees fahrenheit.\n", CUTOFF);
}
} while (temperature < CUTOFF && scanf_result != EOF);
if (scanf_result == EOF) { // try to handle user typing ^D aka EOT
fprintf(stderr, "Program terminated by user.\n");
return 1;
}
/* now do something useful with temperature */
return 0;
}
对 scanf()
返回结果的测试可以防止真正错误的输入:
> ./a.out
Enter temperature in degrees Fahrenheit: dog
You must input a value greater than 67 degrees fahrenheit.
Enter temperature in degrees Fahrenheit: 34
You must input a value greater than 67 degrees fahrenheit.
Enter temperature in degrees Fahrenheit: 103
>
关于c - 当条件满足时,While 循环继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39692983/