c - if else statement not giving expected answer -


the values entered not matching output grade giving 10 grade when conditions 10 not met. issue on entering hardness 50 strength 5600 , carbon 0.7 giving grade 10 while grade 10 carbon should less 0.7? #include #include #include

int main() {     // program grade steel on quality basis      int hardness;     int strength;     float carbon;      printf("enter hardness of steel:");    // condition 1  hardness should >= 50     scanf("%d", &hardness);      printf("enter tensile strength:");     // condition 2  strength should >= 5600     scanf("%d", &strength);      printf("enter carbon content:");           // condition 3  carbon less 0.7     scanf("%.2f", &carbon);       if ((hardness >= 50) && (carbon < 0.7) && (strength >= 5600)) {        // true         printf("\ngrade = 10");     }     else if ((hardness >= 50) && (carbon < 0.7) && (strength <= 5600)) {     // 1 , 2 true         printf("\ngrade = 9");     }     else if ((hardness <= 50) && (carbon < 0.7) && (strength >= 5600)) {    // 2 , 3 true         printf("\ngrade = 8");     }     else if ((hardness >= 50) && (carbon > 0.7) && (strength >= 5600)) {    // 1 , 3 true         printf("\ngrade = 7");     }     else if ((hardness >= 50) && (carbon > 0.7) && (strength <= 5600)) {    // 1 true         printf("\ngrade = 6");     }     else if ((hardness <= 50) && (carbon < 0.7) && (strength <= 5600)) {    // 1 true         printf("\ngrade = 6");     }        else if ((hardness <= 50) && (carbon < 0.7) && (strength >= 5600)) {    // 1 true         printf("\ngrade = 6");     }     else {         printf("\ngrade = 5");                     // none true     }      _getch();     return 0; } 

use of "%.2f" format specifier not correct in scanf. printf not scanf.

it's idea check return value of scanf make sure function able read expected data.

if ( scanf("%.2f", &carbon) != 1 ) {    // deal error. } 

add similar checks other scanf calls.

i think changing above format specifier "%f" should fix problem. add check still.

if ( scanf("%f", &carbon) != 1 ) {    // deal error. } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -