Help - If statement with multiple conditions

This is a discussion on Help - If statement with multiple conditions within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm looking for help with a homework assignment. I have tried writing a code and come across an ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    4

    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.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Use == instead of = to test equality.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    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 '=='?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    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]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple conditions for while
    By sfff in forum C Programming
    Replies: 5
    Last Post: 11-08-2009, 11:10 PM
  2. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 01:41 PM
  3. IF statement - to conditions
    By cornacum in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 08:39 PM
  4. If statement conditions
    By Brewer in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:55 AM
  5. Testing Multiple Conditions
    By awilmut in forum C++ Programming
    Replies: 16
    Last Post: 10-20-2002, 11:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21