Conditional Operator

This is a discussion on Conditional Operator within the C Programming forums, part of the General Programming Boards category; I can not figure out why this program does not work the way I wont. Code: #include <stdio.h> int x, ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Conditional Operator

    I can not figure out why this program does not work the way I wont.
    Code:
    #include <stdio.h>
    
    int x, y;
    
    int main(void)
    { printf("\nEnter two numbers");
    scanf("%d %d",&x,&y);
    printf("\n\n%d is bigger\n",((x>y)?x:y));
    return 0;
    }
    This will compile and run but when you put in two numbers, like 5,50, the program said that 5 is the bigger number.
    Any number on the left of the comma is called the bigger number.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Sorry, but I cannot duplicate your problem. Input of 5 50 gives the expected result that 50 is bigger.

    EDIT:
    Just a moment, did you actually enter 5,50 rather than 5 50?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Try using two scanf statements.

    Code:
    ...
    printf("Input first number: ");
    scanf("%d", &x);
    printf("Input second number: ");
    scanf("%d", &y);
    ...

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It's likely that you entered 5,50 as suggested by laserlight.
    since x and y are global variables, they are initialized to 0.
    scanf() will fail when it reads ',' and return 1(check return value), x will be 5, y will still be 0.
    hence you are getting 5 as result.
    Last edited by Bayint Naung; 10-14-2010 at 12:32 PM. Reason: spelling & grammar misteak

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Thank you for posting, it is still not working for me. Even with splitting the scanf, I still get 5 is bigger than 50. I am going to export the code and run it on the desktop.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    I suggest that you try:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x = 0, y = 0;
        printf("Enter two numbers: ");
        scanf("%d %d", &x, &y);
        if (x != y)
        {
            printf("\n%d is bigger than %d\n", ((x > y) ? x : y), ((x < y) ? x : y));
        }
        else
        {
            printf("\n%d is equal to %d\n", x, y);
        }
        return 0;
    }
    This makes it clear that you are really getting "5 is bigger than 50" rather than "5 is bigger than 0".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Thank You, that is much clearer.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    One last addition before ending this thread. I added a comma to the scanf line.
    Code:
    #include <stdio.h>
    
    int x, y;
    
    int main(void)
    { printf("\nEnter two numbers");
    	scanf("%d,%d", &x, &y);
    	printf("\n\n%d is bigger\n", ((x>y)?x:y));
    	return 0;
    }
    so now it reads %d,%d.

    Once again, thanks for help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help understanding conditional operator
    By Sereby in forum C Programming
    Replies: 7
    Last Post: 08-09-2004, 12:24 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 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