#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    14
    Rep Power
    0

    Relations between numbers


    Hi !
    I am having trouble with this code. It gives error where the "else" statement begins. The code should deliver the relations between integer-values a,b,c. For example : a<=b=c or a<b<c
    I'd be glad if you could help !
    Thanks
    Code:
      #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    int a=0,b=0,c=0;
    printf("Enter three numbers:\n");
     printf("a:");
     scanf("%d",&a);
     printf("b:");
     scanf("%d",&b);
     printf("c:");
     scanf("%d",&c);
    
    if (a<=b) {
        if(b<=c) {
            if(a==b && b==c) {
            printf("%d=%d=%d",a,b,c);}
            else if (a<b && b==c)  {
            printf("%d<%d=%d",a,b,c);}
            else if (a=b && b<c);{
            printf("%d=%d<%d",a,b,c);}
            else {
            printf("%d<%d<%d",a,b,c);
            }
        }
        else if (c<=a) {
            if (c=a && a<b) {
                printf("%d=%d<%d",c,a,b);}
            else if (c<a && a=b) {
                printf("%d<%d=%d",c,a,b);}
            else {
                printf("%d<%d<%d",c,a,b);}
            }
    
    
    }
    
        return 0;
    
    
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > else if (a=b && b<c);
    1. You're using = where you should be using == (not the only place you do this).
    2. You have a mis-placed ; at the end of the line.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    14
    Rep Power
    0
    Thank you so much ! I ve been overlooking these small errors.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481

    read your compiler output!


    In my case, it took some time to understand compiler messages. So true while learning the language. Let's fix the misplaced semi-colon and compile with warnings enabled.
    Code:
    $ gcc -Wall ./c.c 
    ./c.c: In function 'main':
    ./c.c:20:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    ./c.c:27:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    ./c.c:29:26: error: lvalue required as left operand of assignment
    $
    "warning: suggest parentheses around assignment used as truth value" certainly worth investigating, and you did by writing to this forum.

    a=b /* assignment expression */
    a==b /* comparison expression */

    "error: lvalue required as left operand of assignment" say what???
    This is a combination of precedence trouble and the incorrect use of an assignment where comparison was intended.
    Assignment has the lowest precedence of any of the operations in that expression, as if it were parenthesized
    (c<a && a) = (b)
    in other words, store as the int that results from (c<a && a) the value indicated by the variable b. That makes no sense. If you wanted to compute an address you'd have to cast and dereference and take your segmentation fault but we won't go there.
    Last edited by b49P23TIvg; September 22nd, 2013 at 03:43 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Micro Junky
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    NJ
    Posts
    94
    Rep Power
    13
    Your code has an extra ; in the 'else if'

    Code:
    else if (a=b && b<c);{
            printf("%d=%d<%d",a,b,c);}
            else {
    The Rog

IMN logo majestic logo threadwatch logo seochat tools logo