Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Else if not returning value

Rusty

Active Coder
Where is my function going wrong?

Choice 1 returns the correct value of 760
Choice 2 returns an input value of inches properly converted to millimeters.
Choice 3 returns an incorrect value of 760. The math for choice 3 returns a correct value as a standalone function

The last else if executes properly by asking for user input. But the variable atm_press does not take on the value presumably returned by choice 3.

Thanks for your help.

[CODE lang="c" title="initial and call"]double atm_press = 0;
atm_press = get_local_press();[/CODE]

[CODE lang="c" title="Function"]double get_local_press()
{
int response = 0;
double h, temp, input = 0;

printf("\n1\tUse Standard Atmospheric Pressure (inHg) 29.92");
printf("\n2\tUse Local Atmospheric Pressure (inHg)\n");
printf("3\tAltitude and air temperature known\n");
printf("\nPlease enter a choice (1, 2 or 3): ");
scanf("%d", &response);

if (response == 1)
return 760;

else if(response == 2)
{
printf("\n Enter Local Atmospheric Pressure [inHg] ");
scanf("%lf", &input);
input = input * 25.4;
printf("\n input = %.2lf", input);
return input;
}
else if(response == 3)
{
printf("\n Enter Altitude [Meters]:");
scanf("%d", &h);
printf("\n Enter Air Temperature [Celcius]: ");
scanf("%d", &temp);

double a_p = 0;
const double lr = 0.0065; /*lapse rate - change in temperature with altitude*/
const double eq_exp = 5.257; /*exponent for hypsometric equation*/
const double stp = 1013.25; /*Standard Temperature and Pressure (0 deg C at 760 mmHg - Units in kPa*/
const double pa_to_mmhg = 0.750062; /*conversion factor pascals to mmHg*/
const double c_to_k = 273.15; /*Celsius to Kelvin offset*/

return a_p = stp * pow((1 - (lr * h) / (temp + lr * h + c_to_k)), eq_exp) * pa_to_mmhg;
}
}[/CODE]
 
Hi Rusty,

The problem may come from the fact that you read numbers into 'double' variables ('h' and 'temp') with the specifier '%d' (For 'int') instead of '%lf' (For 'double').
 
Last edited:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom