amoeba

This is a discussion on amoeba within the C++ Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { int num1,num2,num3; printf("Enter 3 intergers:"); scanf("%d", &num1); scanf("%d", &num2); scanf("%d", &num3); if( num1 == ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    29

    amoeba

    Code:
    #include <stdio.h>
    int main()
    {
        int num1,num2,num3;
        printf("Enter 3 intergers:");
        scanf("%d", &num1);
        scanf("%d", &num2);
        scanf("%d", &num3);
        if( num1 == num2 == num3 )
           printf("Equal!!");
        else
           printf("Not Equal!!");
        system("pause");
        return 0;
    }
    why can't i get the correct ans?

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    [QUOTE=amoeba532;987150]
    Code:
    #include <stdio.h>
    int main()
    {
        int num1,num2,num3;
        printf("Enter 3 intergers:");
        scanf("%d", &num1);
        scanf("%d", &num2);
        scanf("%d", &num3);
        if( num1 == num2 == num3 )
           printf("Equal!!");
        else
           printf("Not Equal!!");
        system("pause");
        return 0;
    }
    the conditional statement is not correct. num1==num2==num3 is the same as: (num1==num2)==num3. (num1==num2) return 0 or 1 for true or false. Whatever (num1==num2) return will be compared with num3. So in effect, you're not comparing num1, num2 and num3 together. You should not group all conditional test together.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    okk.thx nimitzhunter

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    Quote Originally Posted by amoeba532 View Post
    okk.thx nimitzhunter

    Learn how to use "and," by reading this: http://www.cprogramming.com/tutorial/lesson2.html

    Then learn how to rock "?:" and then learn how to scan for 3 ints without writing 3 lines.

    Here you go, chew on this and see if you can learn more.


    Code:
    #include <stdio.h>
    int main(void){
      int one, two, three;
      printf("Enter 3 ints seperated by spaces:");
      scanf("%d %d %d", &one, &two, &three);
      ((one == two) && (two == three)) ? printf("Equal\n") : printf("Not Equal\n");
      return 0;
    }
    Short, sweet and to the point.

    edited: gave main a return value.
    Last edited by tenchu; 12-02-2010 at 11:30 PM.

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Quote Originally Posted by tenchu View Post
    Learn how to use "and," by reading this: Cprogramming.com Tutorial: If Statements

    Then learn how to rock "?:" and then learn how to scan for 3 ints without writing 3 lines.

    Here you go, chew on this and see if you can learn more.


    Code:
    #include <stdio.h>
    void main(void){
      int one, two, three;
      printf("Enter 3 ints seperated by spaces:");
      scanf("%d %d %d", &one, &two, &three);
      ((one == two) && (two == three)) ? printf("Equal\n") : printf("Not Equal\n");
    }
    Short, sweet and to the point.
    Some compiler may protest "void main()". Standard practice is "int main()"

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    True, but I was keeping the example short and sweet with a focus on the question/point about comparison operators. I edited the original in case it causes confusion.
    Last edited by tenchu; 12-02-2010 at 11:30 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    If you really want to use the ternary operator, I suggest:
    Code:
    #include <stdio.h>
    
    int main(void) {
        int one, two, three;
        printf("Enter 3 ints seperated by spaces:");
        scanf("%d %d %d", &one, &two, &three);
        puts((one == two && two == three) ? "Equal" : "Not Equal");
        return 0;
    }
    But if you want to have two separate printf or puts statements, then I would consider the if and else branches to be clearer as using the ternary operator then looks like obfuscation to me.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    That looks more clear, nice catch!

Popular pages Recent additions subscribe to a feed

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