2016-09-25 4 views
-2

Also schreibe ich ein Engineering-Programm für meine ENGR200-Klasse und ich bin fest stecken. Ich habe noch nie eine Programmiersprache gelernt, aber ich habe das Web für die letzten 3 Stunden durchforstet und jede Variation von Loops getestet, und ich kann scheinbar nichts zur Arbeit bringen.While-Schleife weiter, wenn Bedingung erfüllt

Was will ich das Programm zu tun ist,

  1. eine Benutzereingabe
  2. Test erhalten, wenn sie in einem akzeptablen Bereich liegt
  3. wenn es nicht ich es will
  4. eine Fehlermeldung senden
  5. zur Eingabe eines neuen Eintrags
  6. Wenn es den akzeptablen Bereich erfüllt, möchte ich es auf den Rest fortsetzen.

Mein aktueller Code ist unten:

/******************************************************************************* 
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

Was ist das Problem? – immibis

+3

Wo ist 'temp' definiert? Was ist sein Typ? – user2357112

+3

Bitte erklären Sie, welches Verhalten Sie beobachten und erklären Sie, wie es sich von dem unterscheidet, was Sie erwartet haben. –

Antwort

0

Die folgende tun könnte, was Sie brauchen:

#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; 
} 

Der Test des Rückergebnisses von scanf() schützt vor wirklich schlecht Eingang:

> ./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 
> 
Verwandte Themen