Wrong calculation of percentage

Asked

Viewed 89 times

1

How exactly is it made to use the switch correctly?

Here in the code, the case 1 and 2, they function normally, but in case 3 and 4, at the time of division by 2, or by 3, to know the plots, always gives wrong the result (it seems that is divided by 22 or 23).

Or the switch don’t do this kind of operation? I know you can do it without being a switch, but my teacher wants to.

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

float valorT, valorF, Vparcelas;
int op;

main()
{
    printf("Digite o valor total da sua compra: ");
    scanf("%f", &valorT);
    printf("Digite o numero da opcao de pagamento, estao listadas abaixo: \n");

    printf("1-Dinheiro\n2-Cartao debito\n3-Cartao credito 2x\n4-Cartao credito 3x\n");
    scanf("%d", &op);

    if(op > 4){
        printf("Opcao invalida!\n");
    }

    switch(op){
        case 1: valorF = valorT*0.1;
        printf("O valor total da sua conta ficou: %0.2f", valorT-valorF);
        break;

        case 2: valorF = valorT*0.2;
        printf("O valor total da sua conta ficou: %0.2f", valorT-valorF);
        break;

        case 3: valorF = valorT*0.1;
        Vparcelas = valorF/2; //ISSO DA ERRADO
        printf("O valor total da sua conta ficou: %0.2f,\ne das 3 parcelas: %0.2f\n", valorT+valorF, Vparcelas);
        break;

        case 4: valorF = valorT*0.15;
        Vparcelas = valorF/3; //ISSO TAMBEM
        printf("O valor total da sua conta ficou: %0.2f,\ne das 3 parcelas: %0.2f\n", valorT+valorF, Vparcelas);
        break;
    }



    return 0;


}

3 answers

2


The code has some problems, I will not talk about all, especially the organization (that would help understand the problem) and things that do not matter for an exercise, for example that float does not serve for monetary value.

I understand that the first two want to give a discount (if this is not too weird, the question does not make this clear). The original value is always base 1. So the discount does not need a composite account, just multiply by the percentage of the value of the final value already given the discount, so if it is 10% discount just multiply by 0.9.

For the others I understand that there will be an increase, then it is to do the opposite, multiplies by the value already with the increase, so if it is 10% the value to use will be 1.1.

If you make these simplifications it is easier to understand and then the value account of the plots becomes automatic. Doing something linear avoids problems.

Note that if you typed 0 or negative in the option the code would error as well. And there is no validation if the total value is according to what is expected, but the exercise should not be asking for this.

#include <stdio.h>

int main() {
    float valorT, valorF;
    int op;
    printf("Digite o valor total da sua compra: ");
    scanf("%f", &valorT);
    printf("Digite o numero da opcao de pagamento, estao listadas abaixo: \n");
    printf("1-Dinheiro\n2-Cartao debito\n3-Cartao credito 2x\n4-Cartao credito 3x\n");
    scanf("%d", &op);
    switch (op) {
    case 1:
        printf("O valor total da sua conta ficou: %0.2f", valorT * 0.9);
        break;
    case 2:
        printf("O valor total da sua conta ficou: %0.2f", valorT * 0.8);
        break;
    case 3:
        valorF = valorT * 1.1;
        printf("O valor total da sua conta ficou: %0.2f,\ne das 3 parcelas: %0.2f\n", valorF, valorF / 2);
        break;
    case 4:
        valorF = valorT * 1.15;
        printf("O valor total da sua conta ficou: %0.2f,\ne das 3 parcelas: %0.2f\n", valorF, valorF / 3);
        break;
    default:
        printf("Opcao invalida!\n");
    }
}

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

  • ah, I get it! but I would know how to explain to me because at the time of dividing by 2 or 3 in the code I was dividing by 22 and 23 respectively.. at least by what I tested, pq if I put 1260,23 the result of the 2 plots of 63,01

  • The calculation is all wrong. The formulas are not these, I don’t know where you got it from, so it’s not even worth looking for reasons why.

1

The problem is in your accounts and not in your programming, the switch is nothing more than a multi-condition if.

From your code I imagine:

  • If the customer pays in two installments will have an increase of 10%.
valorF = valorT*0.1; //esse é o valor dos juros
Vparcelas = (valorT + valorF)/2;
  • If the customer pays in three installments will have an increase of 15%.
valorF = valorT*0.15;
Vparcelas = (valorT + valorF)/3;

Also adjust the check of the input value (if not less than one):

if(op > 4 || op < 1){
  printf("Opcao invalida!\n");
}

If this is not so please describe better the problem and what is each variable; and put expected input and output values.

-3

The problem is in the account, you probably made a mistake when placing the multipliers. If you replace 0.x by 1.x it should solve the problem. Or do as Edney said and put it as a somatoria

Browser other questions tagged

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