Confusion with if/else statements

Hello. I am having an issue with my code that I cannot figure out. I have entered an equation and depending on what the result of the equation is, depends on the line of text the program will print on the screen. I will attach a copy of my code with the hopes it is a simple fix and I am just missing something obvious. No matter what the results of my equation is, the program will only print "In the elastic region" I appreciate any advice, thank you.


#include <stdio.h>
#include <math.h>
#define youngs_modulus 28000
#define elastic_limit 0.001527
#define yield_stress 30
#define k 184.923
#define n 0.45

/* Main function */
int main(void)
{
/* Declare variables */
double elastic_region, yielding, strain_hardening, necking, strain, stress;

/* Print headings */
printf("********************************************");
printf("\n STRESS AND STRAIN FOR 304 STAINLESS STEEL");

/* Input values */
printf("\n\n");
printf("Enter strain (in/in): ");
scanf("%lf", &strain);

/* Compute */

stress=k*(pow((strain-elastic_limit),n));

/* Print output values */
printf("\nRESULTS");
printf("\nStress = %7.1f ksi", stress);


/* Determining the strain ranges for the regions*/
if(0.001527>=stress || stress>0)
{
printf("\nIn the elastic region");
}else if(0.002>=stress || stress>0.001527)
{
printf("\nIn the yielding region");
}else if(0.5>=stress || stress>0.002)
{
printf("\nIn the strain hardening region");
}else if(stress>0.5)
{
printf("\nStress cannot be calculated\nIn the necking region");
}




printf("\n********************************************\n\n\n");

return 0;
}
if(0.001527>=stress || stress>0)
well, by this statement ^^^ stress has to be negative or it is elastic.

conditions cost time and cpu effort.
reverse them to condense is easier to read and code:
if stress > 0.5
else
if stress > .002 //here, it can't be > 0.5 so its between .002 and .5
else
if stress > .001527 //here it cant be > .002, so its between again...

Last edited on
Yes, that looks much cleaner and easier on the eye. Thank you very much!
Topic archived. No new replies allowed.