How to simplify a problem to get the names of the months of the year?

Asked

Viewed 223 times

8

I am beginning to program in C and I realized this problem whose statement is the following:

Write an exercise that asks the user for a one-month number and prints the name of the month on the screen. For example, the user enters 1 and the program writes "January". If you enter a number that does not match any month, you should write an appropriate error message on the screen.

For now, I only know the basics (if, else...), so I did so:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int mes;
    printf("Numero do mes: \n");
    scanf("%d",&mes);
    if (mes == 1)
        printf("Janeiro\n");
    else if (mes == 2)
        printf("Fevereiro\n");
    else if (mes == 3)
        printf("Marco\n");
    else if (mes == 4)
        printf("Abril\n");
    else if (mes == 5)
        printf("Maio\n");
    else if (mes == 6)
        printf("Junho\n");
    else if (mes == 7)
        printf("Julho\n");
    else if (mes == 8)
        printf("Agosto\n");
    else if (mes == 9)
        printf("Setembro\n");
    else if (mes == 10)
        printf("Outubro\n");
    else if (mes == 11)
        printf("Novembro\n");
    else if (mes == 12)
        printf("Dezembro\n");
    else if ((mes<1) || (mes>12))
        printf("Erro\n");

    return 0;

}

How can I solve this same problem with a more simplified code?

  • Search by switch/case command.

  • Note that in your program you test all valid month numbers (1 to 12) so there is no need for the last if.

  • 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

3 answers

7

Some things are left over, but the biggest simplification is to use a array in place of a if. You can put all the names on this array and access through its index according to typing. Validation needs to be done first. Note that I removed some redundancy, after all simplification entail this.

#include <stdio.h>

int main() {
    int mes;
    printf("Numero do mes: ");
    scanf("%d", &mes);
    if (mes < 1 || mes > 12) {
        printf("\nErro");
        return 0;
    }
    printf("\n%s", (char *[12]){ "Janeiro", "Fevereiro", "Marco", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro" }[mes - 1]);
}

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

  • 1

    It would not be better to use a function that returns the chosen month?

  • 1

    @Cat, I don’t know, it depends on what you want to do, in an exercise that almost walks and wants to be simple a function seems to be cannon to kill bird. There is a strong justification for using the function?

  • 1

    Increase readability and avoid repeating the array, the principle of the function is to just return the month chosen.

  • 1

    There is no repetition, readability is debatable. In an exercise that wants to be simple the act of creating a function worsens readability, and is overengineering.

5

Store months in an array: const char *strings[] = {"Janeiro","Fevereiro"...};, and access the respective month with user input:

printf("Numero do mes: \n");
scanf("%d",&mes);

printf("%s", meses[mes-1]);
  • Why is this "i" on the printf? It is not set...

  • Damn! I didn’t even notice.

-1

 #include <stdio.h>

 int main() {
    int mes;
    printf("Numero do mes: ");
    scanf("%d", &mes);
    const char *meses[] = {"Janeiro", ...};

    for(int i = 0; i < 12; i++) {
        if(mes == i) {
            printf(meses[i]);
        }
    }
 }
  • Hi, Juliana, take a look at this topic and understand why you can take negative points with answers in the format of your (In code only): https://pt.meta.stackoverflow.com/questions/7634/voc%C3%Aas-think-this-right? cb=1

Browser other questions tagged

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