if-Else command is not working

Asked

Viewed 363 times

2

In one of the exercises of a book I’m reading, I’m asked to trace a person’s "profile" according to the year she was born (like those Facebook tests) but in order to limit the possibility of what is typed, I put a if to signal an error, but even with the value being positive for the if, the block of else keeps running, how can I solve?

int main(void){
    setlocale(LC_ALL,"");
    int y,a,b,c;
    printf("Digite seu ano de nascimento (quatro digitos)\n");
    scanf("%d",&y);
    if (y>9999 && y<1000) printf("Ano inválido");
        else {
        b=y%100;
        a=y/100;
        c=a+b;
        c=c%5;
        switch(c){
            case 0:printf("Tímido\n"); break;
            case 1:printf("Sonhador\n"); break;
            case 2:printf("Paquerador\n"); break;
            case 3:printf("Atraente\n"); break;
            case 4:printf("Irresistível\n"); break;
                }
            }
        return 0;
    }
  • What is your problem?

  • even putting a year like 20000 or 3, still is given a result of the Else block

  • did not understand, but in the question were cut some words of what I put, so it was difficult to understand

  • @If you’re talking about my edition, I haven’t removed anything else. You can confirm this in the history.

  • it wasn’t your issue, actually a few words I put in the question in the appeared

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

Show 1 more comment

2 answers

2

This line

if (y>9999 && y<1000) printf("Ano inválido");

says: If Y is greater than 9999 and less than 1000 print "Invalid year"

This condition is always false. That is, the else ALWAYS RUNS.

Try it like this:

if (y>9999 || y<1000) printf("Ano inválido");

2

The comparison is using a and && when the right one would be a or ||, since he is invalid both. In fact it would be impossible for both to be invalid, because **or ** the number is greater than 9999 or it is less than 1000, there is no way that a number can be both at the same time, which is a requirement of and.

I gave an improved, but could improve more, could give better names for variables and even eliminate some.

#include <stdio.h>

int main(void) {
    int y = 0;
    printf("Digite seu ano de nascimento (quatro digitos)\n");
    scanf("%d", &y);
    if (y > 9999 || y < 1000) { 
        printf("Ano inválido");
    } else {
        int b = y % 100;
        int a = y / 100;
        int c = a + b;
        c %= 5;
        switch (c) {
            case 0: printf("Tímido\n"); break;
            case 1: printf("Sonhador\n"); break;
            case 2: printf("Paquerador\n"); break;
            case 3: printf("Atraente\n"); break;
            case 4: printf("Irresistível\n"); break;
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

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