Beginner at C programming And operator and if statement Problem!

This is a discussion on Beginner at C programming And operator and if statement Problem! within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main( ) { int c, h, o, n, charge = 0; float weight; c = 3, ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    Exclamation Beginner at C programming And operator and if statement Problem!

    Code:
    #include <stdio.h>
    int main( ) {
       int c, h, o, n, charge = 0;
       float weight;
       c = 3, h = 7, n = 1, o = 2, weight = 89.0929;
       if ((c == 3) && (h == 7) && (n == 1) && (o == 2) && (weight == 89.0929)){printf ("Alanine, C:%d H:%d N:%d O:%d, weight:%.4f charge:%d\n", c, h, n, o, weight, charge);}
    return 0;
    }
    i am not sure what the problem is but it won't display the printf. The if statement seem to be correct so for.. when i compile it there is no errors.

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    10
    Try with these fixes,
    Note that each off these assignment should each be on separate line
    c = 3, h = 7, n = 1, o = 2, weight = 89.0929;

    Code:
    #include <stdio.h>
    int main( ) {
       int c=3, h=7, o=2, n=1, charge = 0;
    
       float weight;
    
       weight = 89.0929;
    
    
       if ((c == 3) && (h == 7) && (n == 1) && (o == 2) && (weight == 89.0929))
       {
          printf ("Alanine, C:%d H:%d N:%d O:%d, weight:%.4f charge:%d\n", c, h, n, o, weight, charge);
       }
    
    
       return 0;
    
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,507
    Floating Point
    All floats are approximations, so it's unwise to try to compare them using ==

    For example, weight = 89.0929; and weight == 89.0929 involves the initial demotion of a double constant to a float variable in the assignment, then the promotion of a float variable to a double to perform the comparison.

    You might be able to get away with this, which forces the constants to be floats to begin with
    Code:
       weight = 89.0929f;
      
       if ((c == 3) && (h == 7) && (n == 1) && (o == 2) && (weight == 89.0929f))
    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.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Quote Originally Posted by Salem View Post
    Floating Point
    All floats are approximations, so it's unwise to try to compare them using ==

    For example, weight = 89.0929; and weight == 89.0929 involves the initial demotion of a double constant to a float variable in the assignment, then the promotion of a float variable to a double to perform the comparison.

    You might be able to get away with this, which forces the constants to be floats to begin with
    Code:
       weight = 89.0929f;
      
       if ((c == 3) && (h == 7) && (n == 1) && (o == 2) && (weight == 89.0929f))
    that works great thank you!! i came across another problem in this set of code
    Code:
       do{       
           if (weight <= 204.2247){
           years += 1;
           c = 3, h = 7, n = 1, o = 2, weight = 89.0929, charge = 0;
           }
           else if (weight > 204.2247){
               c = h = o = n = charge = 0;
               weight = 0.0;
               }
    printf ("Alanine, C:%d H:%d N:%d O:%d, weight:%.4f charge:%d", c, h, n, o, weight, charge);
                   
       }while (years != 20 || (charge != 0 && weight >= 204.2247));
    i want it to end loop once the charge = 0 and weight <= 204.2247.
    Last edited by iKevinChau; 12-10-2012 at 09:05 AM.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Then the test for continuing the loop should be the opposite of when to terminate.
    You said you wanted it to end on charge = 0 and weight <= 204.2247... so the logic to invert that for continuing loop is: charge != 0 || weight > 204.2247. Note the change from 'and' to 'or' and the boolean inverse of <= is >.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Thanks a lot!! this is my last problem.. I was wondering if my code is right to printf the statement.. it doesn't work atm.
    Code:
    #include <stdio.h>int moleculeFound(float weight,int charge, int c, int h, int o, int n);
    int main( ) {
       int c, h, o, n, atom,amino =0, years = 0, charge = 0;
       float weight;
    c == 3, h == 7, n == 1, o == 2, weight == 89.0929f, charge = 0;
    moleculeFound(weight, charge, c, h, o, n);    
    return 0;
    }
    int moleculeFound(float weight, int charge, int c, int h, int o, int n){
    if ((c == 3) && (h == 7) && (n == 1) && (o == 2) && (weight == 89.0929f)){printf ("C:%d H:%d N:%d O:%d, weight:%.4f charge:%d", c, h, n, o, weight, charge);}
    }

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    Code:
    c == 3, h == 7, n == 1, o == 2, weight == 89.0929f, charge = 0;
    You're mixing up the equality operator (==) with the assignment operator (=).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner If Statement Help
    By shukiren in forum C Programming
    Replies: 3
    Last Post: 09-06-2011, 02:05 PM
  2. Beginner: Problem with If-Else Statement
    By matrixx333 in forum C Programming
    Replies: 3
    Last Post: 08-16-2009, 09:36 PM
  3. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  4. Beginner needs help with If Statement
    By Sahaal in forum C++ Programming
    Replies: 7
    Last Post: 12-23-2006, 03:17 PM
  5. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM

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