Like Tree1Likes

If else if stament help

This is a discussion on If else if stament help within the C Programming forums, part of the General Programming Boards category; from what I understand of this this code I created using if else if statement (assignment) seems correct to me. ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    33

    If else if stament help

    from what I understand of this this code I created using if else if statement (assignment) seems correct to me.
    but for some reason it is not printing L or
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
        clrscr();
        int x,y,z,l;
        printf("Enter value for x: ");
        scanf("%d",&x);
        printf("Enter value for y: ");
        scanf("%d",&y);
        printf("Enter value for z: ");
        scanf("%d",&z);
        if (x>y)
            l = x;
        else if (y>z)
            l = y;
        else if (z>x)
            l = z;
        else
            printf("The large number is %d", l);
        getch();
        return(0);
    }
    Last edited by langamer101; 01-18-2012 at 06:12 AM.

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Why have you put the printf inside the else? It will only get executed if x <= y and y <= z and z <= x (i.e. it will never print unless they all the same number).

    Scrap the else (that said, I have no idea why you've used if/else to do... what? Find the largest number?)

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,768
    You either set the value of "l" or output the value of "l".
    Please show the sequence that results in both setting the value and outputting it.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    so I don't put printf inside else. ok let me try

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    seems to me that I have no idea of putting printf outside of else @ledow so I will try @stahta01 suggestion.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    for some reason if i put printf inside "else if" it dont work just goes directly to else? what the heck.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    66
    If x,y and z are equal, it will go to else part.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    so i redo everything here is code
    it works but still there is problem in y or z
    if i input in x as 1, y as 32, z as 3
    the output goes directly to z or 3. wth?
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
        clrscr();
        int x,y,z;
        printf("Enter value for x: ");
        scanf("%d",&x);
        printf("Enter value for y: ");
        scanf("%d",&y);
        printf("Enter value for z: ");
        scanf("%d",&z);
        if (x>y)
            printf("The large number is %d", x);
        else if (z>x)
            printf("The large number is %d", z);
        else if (y>x)
            printf("The large number is %d", y);
        else
            printf("all values are the same");
            getch();
        return(0);
    }
    Last edited by langamer101; 01-18-2012 at 06:53 AM.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Your problem is what you want, is for example to print the value of x if x is the biggest number, not only if x is bigger than y (same for the other conditions). So what you should test, is if x is bigger than y AND bigger than z, which would make it the biggest number between x, y and z.
    HomePort : A C Web Service API for heterogeneous home automation systems

  10. #10
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    66
    Recheck your logic which you implemented.
    For i/p: x=10; y=5; z=15;
    what will be the o/p of your program?
    stahta01 likes this.

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    It seems Tibo-88 suggestion worked for me thanks and thanks for all the help guys
    so here is code redone
    so I tried calculating true or false and saw that "z > x" and "y > x" was wrong.
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
        clrscr();
        int x,y,z,l;
        printf("Enter value for x: ");
        scanf("%d",&x);
        printf("Enter value for y: ");
        scanf("%d",&y);
        printf("Enter value for z: ");
        scanf("%d",&z);
        if (x>y)
            printf("The large number is %d", x);
        else if (z>y)
            printf("The large number is %d", z);
        else if (y>x)
            printf("The large number is %d", y);
        else
            printf("All values are the same %d", l);
            getch();
            return(0);
    }
    Last edited by langamer101; 01-18-2012 at 07:22 AM.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    I'm happy if you succeed to make it work, but it seems like the code you just posted is exactly the same than before !
    HomePort : A C Web Service API for heterogeneous home automation systems

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    Its looks exactly the same but its not in logic.
    so basically i started looking where would the large number go if true or false
    if x > y it would print x if false goes to next else if
    if z > y it would print z if false goes to next else if
    if y > x it would print y if false goes to x. but thats how I solved it.
    the program now works.

  14. #14
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    I'm sorry to disappoint you, but the logic is still not good. What if x > y but z > x? Then your program will print x, even if z is bigger than x.
    HomePort : A C Web Service API for heterogeneous home automation systems

  15. #15
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    66
    Quote Originally Posted by langamer101 View Post
    Its looks exactly the same but its not in logic.
    so basically i started looking where would the large number go if true or false
    if x > y it would print x if false goes to next else if
    if z > y it would print z if false goes to next else if
    if y > x it would print y if false goes to x. but thats how I solved it.
    the program now works.
    what will be the o/p of for the below conditions, as per your logic?
    i/p: x=10; y=5; z=15;
    Recheck your logic as per post#9

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

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