Error printing message inside condition

Asked

Viewed 42 times

0

When I type any value other than 2 equal values, the "Invalid typed value!" message is displayed anyway. Anyone knows why?

#include <stdio.h>
#include <conio.h>

    int main(){
        int x, y;

        printf("Digite 2 valores: ");
        scanf("%d %d", &x, &y);

        if(x>y)
            printf("O valor %d eh maior que %d", x, y);
        if(x<y)
            printf("O valor %d eh menor que %d", x, y);
        if(x==y)
            printf("O valor %d eh igual ao valor %d", x, y);
        else
            printf("Valor digitado invalido!");     

        getch();
        return 0;
    }

1 answer

0


You must use else if instead of repeating the if

correcting:

#include <stdio.h>
#include <conio.h>

    int main(){
        int x, y;

        printf("Digite 2 valores: ");
        scanf("%d %d", &x, &y);

        if(x>y)
            printf("O valor %d eh maior que %d", x, y);
        else if(x<y)
            printf("O valor %d eh menor que %d", x, y);
        else if(x==y)
            printf("O valor %d eh igual ao valor %d", x, y);
        else
            printf("Valor digitado invalido!");

        getch();
        return 0;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.