2016-09-25 92 views
-2

所以,我正在爲我的ENGR200類寫一個工程類型程序,我徹底卡住了。我從來沒有使用過編程語言,但是我已經在網上搜索了最近3個小時的測試等等每一個循環的變體,我似乎無法得到任何工作。雖然條件符合時循環繼續

我想要什麼程序做的是

  1. 接收用戶輸入
  2. 測試它是否處在可接受的範圍
  3. 內,如果它不我希望它發送一個錯誤信息
  4. 提示新條目
  5. 如果它符合我希望它繼續其餘的可接受範圍。

我當前的代碼如下:

/******************************************************************************* 
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; 
} 
/*********************************************************************/ 
+3

什麼問題? – immibis

+3

定義了「temp」的位置?它的類型是什麼? – user2357112

+3

請解釋你觀察到的行爲,並解釋它與預期的不同之處 –

回答

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 
>