while for number other than 0 in C

Asked

Viewed 1,286 times

2

I need the program to stop when the consumer number is 0. So I put one of the while saying that as long as the consumer number is different from 0 he continues the program and when the consumer number is 0, he finishes the program. It turns out that even typing the 0, the program continues.

#include <stdio.h>
int main()
{
    struct pessoas
    {

        int codTC; //codigo do consumidor para falar se é 1-Residencial 2-Comercial 3-Industrial
        int num; //numero do consumidor (vou colocar o numero referente ao cod para nao confundir)
        float kwh; //Quilowatt-hora

    };

    struct pessoas consumidor[16];

    float total, totalT, consumo1, consumo2, media;
    int i;

    printf("\nExercício desenvolvido considerando a tarifa do CIP - Contribuição Municipal\n");

    do
    {

        printf("\nTipo de consumidor, digite:\n1 - Residencial\n2 - Comercial\n3 - Industrial: ");
        scanf("%i", &consumidor[i].codTC);
        fflush(stdin);

        if (consumidor[i].codTC == 1)
        {

            printf("\nResidencial");

            printf("\n\nDigite o numero do consumidor: ");
            scanf("%i", &consumidor[i].num);

            printf("\nDigite a quantidade de kWh consumidos durante o mes: ");
            scanf("%f", &consumidor[i].kwh);

            //O total do valor sem tarifa, ou seja o custo total para cada consumidor;
            total = consumidor[i].kwh * 0.3;

            //O total do valor com tarifa, ou seja o total + tarifas
            totalT = total + 5.17;

            consumo1 = consumidor[i].kwh;


        }
        else if (consumidor[i].codTC == 2)
        {

            printf("\nComercial");

            printf("\n\nDigite o numero do consumidor: ");
            scanf("%i", &consumidor[i].num);

            printf("\nDigite a quantidade de kWh consumidos durante o mes: ");
            scanf("%f", &consumidor[i].kwh);

            total = consumidor[i].kwh * 0.5;

            //O total do valor com tarifa, ou seja o total + tarifas
            totalT = total + 5.17;

            consumo2 = consumidor[i].kwh;



        }
        else if (consumidor[i].codTC == 3)
        {

            printf("\nIndustrial");

            printf("\n\nDigite o numero do consumidor: ");
            scanf("%i", &consumidor[i].num);

            printf("\nDigite a quantidade de kWh consumidos durante o mes: ");
            scanf("%f", &consumidor[i].kwh);

            total = consumidor[i].kwh * 0.7;

            //O total do valor com tarifa, ou seja o total + tarifas
            totalT = total + 5.17;

        }

        media = consumo1 + consumo2 / 2;


        printf("\nO total de consumo e %.2f\n", total);
        printf("\nO custo total e %.2f\n", totalT);
        printf("\nA media de consumo dos consumidores 1 e 2 e %.2f\n", media);

    } while(consumidor[i].num != 0);


    return(0);
}

3 answers

3

The code doesn’t even compile. The variable i does not vary. There are several situations where an error can occur there and there are things in the code that I would do very different, but I will only solve the problem presented:

#include <stdio.h>
int main() {
    struct pessoas {
        int codTC; //codigo do consumidor para falar se é 1-Residencial 2-Comercial 3-Industrial
        int num; //numero do consumidor (vou colocar o numero referente ao cod para nao confundir)
        float kwh; //Quilowatt-hora
    };
    struct pessoas consumidor[16];
    float total, totalT, consumo1, consumo2;
    int i = 0;
    printf("\nExercício desenvolvido considerando a tarifa do CIP - Contribuição Municipal\n");
    do {
        printf("\nTipo de consumidor, digite:\n1 - Residencial\n2 - Comercial\n3 - Industrial: ");
        scanf("%i", &consumidor[i].codTC);
        fflush(stdin);
        if (consumidor[i].codTC == 1) {
            printf("\nResidencial");
            printf("\n\nDigite o numero do consumidor: ");
            scanf("%i", &consumidor[i].num);
            printf("\nDigite a quantidade de kWh consumidos durante o mes: ");
            scanf("%f", &consumidor[i].kwh);
            //O total do valor sem tarifa, ou seja o custo total para cada consumidor;
            total = consumidor[i].kwh * 0.3;
            //O total do valor com tarifa, ou seja o total + tarifas
            totalT = total + 5.17;
            consumo1 = consumidor[i].kwh;
        } else if (consumidor[i].codTC == 2) {
            printf("\nComercial");
            printf("\n\nDigite o numero do consumidor: ");
            scanf("%i", &consumidor[i].num);
            printf("\nDigite a quantidade de kWh consumidos durante o mes: ");
            scanf("%f", &consumidor[i].kwh);
            total = consumidor[i].kwh * 0.5;
            //O total do valor com tarifa, ou seja o total + tarifas
            totalT = total + 5.17;
            consumo2 = consumidor[i].kwh;
        } else if (consumidor[i].codTC == 3) {
            printf("\nIndustrial");
            printf("\n\nDigite o numero do consumidor: ");
            scanf("%i", &consumidor[i].num);
            printf("\nDigite a quantidade de kWh consumidos durante o mes: ");
            scanf("%f", &consumidor[i].kwh);
            total = consumidor[i].kwh * 0.7;
            //O total do valor com tarifa, ou seja o total + tarifas
            totalT = total + 5.17;
        }
        float media = consumo1 + consumo2 / 2;
        printf("\nO total de consumo e %.2f\n", total);
        printf("\nO custo total e %.2f\n", totalT);
        printf("\nA media de consumo dos consumidores 1 e 2 e %.2f\n", media);
        i++;
    } while (consumidor[i - 1].num != 0);
}

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

A slightly better solution but still with problems:

#include <stdio.h>
int main() {
    struct pessoas {
        int codTC; //codigo do consumidor para falar se é 1-Residencial 2-Comercial 3-Industrial
        int num; //numero do consumidor (vou colocar o numero referente ao cod para nao confundir)
        float kwh; //Quilowatt-hora
    };
    struct pessoas consumidor[16];
    float total, consumo1, consumo2;
    int i = 0;
    printf("Exercício desenvolvido considerando a tarifa do CIP - Contribuição Municipal\n");
    do {
        printf("\nDigite o numero do consumidor: ");
        scanf("%i", &consumidor[i].num);
        if (consumidor[i].num == 0) break;
        printf("\nTipo de consumidor, digite:\n1 - Residencial\n2 - Comercial\n3 - Industrial: ");
        scanf("%i", &consumidor[i].codTC);
        printf("\nDigite a quantidade de kWh consumidos durante o mes: ");
        scanf("%f", &consumidor[i].kwh);
        fflush(stdin);
        if (consumidor[i].codTC == 1) {
            printf("\nResidencial");
            total = consumidor[i].kwh * 0.3;
            consumo1 = consumidor[i].kwh; //isto não faz sentido mas deixei
        } else if (consumidor[i].codTC == 2) {
            printf("\nComercial");
            total = consumidor[i].kwh * 0.5;
            consumo2 = consumidor[i].kwh; //isto não faz sentido mas deixei
        } else if (consumidor[i].codTC == 3) {
            printf("\nIndustrial");
            total = consumidor[i].kwh * 0.7;
        }
        float totalT = total + 5.17;
        float media = consumo1 + consumo2 / 2; //isto não faz sentido mas deixei
        printf("\nO total de consumo e %.2f\n", total);
        printf("\nO custo total e %.2f\n", totalT);
        printf("\nA media de consumo dos consumidores 1 e 2 e %.2f\n", media);
        i++;
    } while (i == 16);
}

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

  • In the codeblock in my note compiled normally, I understood what you did. Thanks for the help. I’m a beginner, I have a lot to learn yet, that’s why it’s very different. Thanks anyway! ;)

  • The solution you have chosen does not even do what is stated and it does not actually solve the problem you have. But it’s your choice to choose whatever you want to get the wrong solution.

  • And the fact of compiling in a compiler, does not mean that it is right, only that this compiler let pass errors that will bring problems in any non-trivial situation, ie, is learning the wrong way.

  • I get it, it’s always good to learn more. Thank you ;)

2


Are you reading to consumidor[i].codTC but to verify consumidor[i].num.

scanf("%i", &consumidor[i].codTC);

// ...

while(consumidor[i].num != 0)

You only read to consumidor[i].num when the first reading is 1, 2, or 3. If the first reading is 0, the consumidor[i].num has an invalid value.

  • Ah, I get it. Wow, thank you very much. A mistake so simple it took me a few hours trying to solve. Thank you very much! ;)

2

The problem here is that at the beginning of your of the .... while you are storing the entered value within consumer[i]. codTC and in your while you validate if the value of consumer[i]. in a is different from zero.

But as the value typed in consumer[i]. codTC was zero it does not pass in any of its ifs and then does not arrow the value zero within consumer[i]. in a thus making your while return false and continue the execution.

Browser other questions tagged

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