Switch command does not work and shows no errors

Asked

Viewed 147 times

1

I’m trying to solve the following exercise: Devise an algorithm that assists in controlling a cattle farm that has a total of 2,000 head of cattle. The database consists of a set of structures (records) containing the following fields for each head of cattle:

• code: cattle head code

• milk: number of litres of milk produced per week;

• Alim: amount of food ingested per week - in kilograms;

• birth: date of birth - month and year;

• slaughter: ạ N" (no) or’S' (yes).

The field nasc. is of type struct data which in turn, has two fields:

• month

• year

Develop functions to:

a) Read the database (code, milk, Alim, nasc.mês and nasc.ano), stored in a vector of structures.

b) Complete the slaughter field, whereas the head of livestock will be slaughtered if:

• is more than 5 years old, or;

• produces less than 40 litres of milk per week, or

• produces between 50 and 70 litres of milk per week and ingests more than 50 kilograms of food per day.

c) Create the options menu for:

C1.Return the total amount of milk produced per week on the farm.

C2. Return the total amount of food consumed per week on the farm.

C3.Return the total amount of milk that will be produced per week on the farm after slaughter.

C4. Return the total amount of food that will be consumed per week on the farm after slaughter

C5. Return number of livestock going to slaughter.

C6. Exit from the programme.

However, I am having problems with the Switch command, my code is not displaying any message, either error or warning.

Follow the code below:

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

struct nascimento
{
    int mes;
    int ano;
};

struct registro
{
    int codigo;
    int leite;
    int alimento;
    char abate[2];
    struct nascimento nasc;
};

struct registro gadovet[2];

void registrar_gado(struct registro gadovet[5];)
{
    int i;
    for (i = 0;i < 2; i++)//registro dos 2000 gado
    {
        printf("Digite o código do gado: ");
        scanf("%d", &gadovet[i].codigo);
        printf("Digite a quantidade de leite que o gado fornece por semana - em litros: ");
        scanf("%d", &gadovet[i].leite);
        printf("Digite a quantidade de alimento ingerida por semana - em quilos: ");
        scanf("%d", &gadovet[i].alimento);
        printf("Digite a data de nascimento do gado\n");
        printf("Mês: ");
        scanf("%d", &gadovet[i].nasc.mes);
        printf("Ano: ");
        scanf("%d", &gadovet[i].nasc.ano);

        //verificação de abate
        if (gadovet[i].leite >= 50 && gadovet[i].leite <= 70 && gadovet[i].alimento > 50)
        {
            if (gadovet[i].nasc.ano < 2013 || gadovet[i].leite < 40)
            {
                strcpy (gadovet[i].abate, "S");
            }
            else
            {
                strcpy (gadovet[i].abate, "N");
            }
        }
        if (gadovet[i].nasc.ano > 2013 || gadovet[i].leite > 40)
            {
                strcpy (gadovet[i].abate, "N");
            }
    }
}

int main()
{
    setlocale(LC_ALL,"Portuguese");
    int i, x = 0, n = 0, abate = 0;
    char sim[2] = {"S"};
    char nao[2] = {"N"};

    registrar_gado(gadovet[2]);

    do//Menu
    {
        printf("Escolha uma das opções:");
        printf("\n1 - Verificar a quantidade total de leite produzida por semana na fazenda.");
        printf("\n2 - Verificar a quantidade total de alimento consumido por semana na fazenda.");
        printf("\n3 - Verificar a quantidade total de leite que vai ser produzido por semana na fazenda, após o abate.");
        printf("\n4 - Verificar a quantidade total de alimento que vai ser consumido por semana na fazenda, após o abate.");
        printf("\n5 - Verificar o número de cabeças de gado que iram para o abate.\n\n");
        scanf("%d", &n);

    }while(n = 0);

    switch (n)
    {
        case 1://Leite por semana
            for (i = 0;i < 2; i++)
            {
                x = x + gadovet[i].leite;
            }
            printf("A quantidade de leite que o todo o gado oferece por semana é de %d Litros.", x);
            break;
        case 2://Alimento por semana
            for (i = 0;i < 2; i++)
            {
                x = x + gadovet[i].alimento;
            }
            printf("A quantidade de alimento que o todo o gado consome por semana é de %d Quilos.", x);
            break;
        case 3://Leite após abate
            for (i = 0;i < 2; i++)
            {
                if (!strcmp(gadovet[i].abate, nao))
                {
                    x = x + gadovet[i].leite;
                }
            }
            printf("A quantidade de Leite que será consumido por semana após o abate será de %d Litros.", x);
            break;
        case 4://Alimento após abate
            for (i = 0;i < 2; i++)
            {
                if (strcmp(gadovet[i].abate, nao))
                {
                    x = x + gadovet[i].alimento;
                }
            }
            printf("A quantidade de Alimento que será consumido por semana após o abate será de %d Quilos.", x);
            break;
        case 5://Cabeças pro abate
            for (i = 0;i < 2; i++)
            {
                if (!strcmp(gadovet[i].abate, sim))
                {
                    abate++;
                }
            }
            printf("A quantidade de cabeças que irão para o abate é de %d", abate);
            break;
        case 6:
            printf("Pressione qualquer tecla para sair...");
            system("Pause");
            break;
    }
}
  • I haven’t put 2000 in the counters yet and the vector that’s to test faster, then I have to remember to change this.

1 answer

2


On the line where the command is do...while, the condition is as n = 0, try to trade for n == 0.

  • 1

    Thank you very much, that’s right.

Browser other questions tagged

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