# Help - If statement with multiple conditions

Printable View

• 02-26-2013
trintiin
Help - If statement with multiple conditions
Hi everyone, I'm looking for help with a homework assignment. I have tried writing a code and come across an issue where I'm not getting the desired result. It is to design a program that tells whether gas emissions from a car are too high, or permissible. The instructions are to design a code where there are four possible choices of pollutant, whether the emitted pollutant ratio is greater or less then a certain value, and the mileage on the car. All of these are supposed to determine whether the emissions are permissible or not, which would be displayed with printf. (The actual conditions of the assignment are described perfectly by the code below, so I didn't think it would be necessary to write it out. The problem is that I'm getting a logical error.) Here's the code:

Code:

```#include <stdio.h> int main() {     int poln, odr;     double gpm;         printf ("(1) Carbon Monoxide\n(2) Hydrocarbons\n(3) Nitrogen Oxides\n(4)Nonmethane Hydrocarbons\n\nEnter the pollutant number >>\n");     scanf ("%d", &poln);     printf ("Enter the number of grams emitted per mile >>\n");     scanf ("%lf", &gpm);     printf ("Enter the odometer reading >>\n");     scanf ("%d", &odr);         if (poln = 1 && gpm > 3.4 && odr < 50000)             printf("\nEmissions exceed permitted level of 3.4 grams per mile.");     else         if (poln = 1 && gpm < 3.4 && odr < 50000)             printf("\nEmission levels are permissible.");     else         if (poln = 1 && gpm > 4.2 && odr > 50000)             printf("\nEmissions exceed permitted level of 4.2 grams per mile.");     else         if (poln = 1 && gpm < 4.2 && odr > 50000)             printf("\nEmission levels are permissible.");         else             if (poln = 2 && gpm > 0.31 && odr < 50000)                 printf("\nEmissions exceed permitted level of 0.31 grams per mile.");         else             if (poln = 2 && gpm < 0.31 && odr < 50000)                 printf("\nEmission levels are permissible.");         else             if (poln = 2 && gpm > 0.39 && odr > 50000)                 printf("\nEmissions exceed permitted level of 0.39 grams per mile.");         else             if (poln = 2 && gpm < 0.39 && odr > 50000)                 printf("\nEmission levels are permissible.");         else                 if (poln = 3 && gpm > 0.4 && odr < 50000)                     printf("\nEmissions exceed permitted level of 0.4 grams per mile.");             else                 if (poln = 3 && gpm < 0.4 && odr < 50000)                     printf("\nEmission levels are permissible.");             else                 if (poln = 3 && gpm > 0.5 && odr > 50000)                     printf("\nEmissions exceed permitted level of 0.5 grams per mile.");             else                 if (poln = 3 && gpm < 0.5 && odr > 50000)                     printf("\nEmission levels are permissible.");             else                     if (poln = 4 && gpm > 0.25 && odr < 50000)                         printf("\nEmissions exceed permitted level of 0.25 grams per mile.");                 else                     if (poln = 4 && gpm < 0.25 && odr < 50000)                         printf("\nEmission levels are permissible.");                 else                     if (poln = 4 && gpm > 0.31 && odr > 50000)                         printf("\nEmissions exceed permitted level of 0.31 grams per mile.");                 else                     if (poln = 4 && gpm < 0.31 && odr > 50000)                         printf("\nEmission levels are permissible.");         return (0); }```
I tried to write it as neat as possible, I apologize if this somehow violates the forum rules, and did not intend to. My error is that the program will proceed with the first four if/else statements, but once the 'poln' value changes to something other then one, it will not display the expected quotation. If anyone could give me some advice, I'd really appreciate it. Thanks.
• 02-26-2013
TheBigH
Use == instead of = to test equality.
• 02-26-2013
trintiin
Haha, wow, it works now. I feel like I've wasted your time seeing what a simple mistake that was. Why was the '=' the problem opposed to using '=='?
• 02-27-2013
Salem
Because = is assignment, and == is comparison.

Consider turning up the warning level on your compiler.
Code:

```\$ gcc -Wall bar.c bar.c: In function ‘main’: bar.c:18:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:21:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:24:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:27:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:30:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:33:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:36:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:39:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:42:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:45:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:48:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:51:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:54:21: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:57:21: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:60:21: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bar.c:63:21: warning: suggest parentheses around assignment used as truth value [-Wparentheses]```