Price calculation algorithm does not display the result correctly

Asked

Viewed 66 times

0

I’m trying to make an algorithm to calculate what should be paid for a product, considering the normal price tag and the choice of payment condition, but it can not calculate, is displaying the value 100.

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

void main()
{
    float valor;
    int cod_pagamento;

    printf("Insira o valor do produto: R$");
    scanf("%f", &valor);

    printf("Insira a forma de pagamento: ");
    scanf("%d", &cod_pagamento);

     switch(cod_pagamento){
        case 1:
            printf("Voce vai pagar R$ %.2f", valor-((10/100)*valor));
            break;
        case 2:
            printf("Voce vai pagar R$ %.2f", valor-((5/100)*valor));
            break;
        case 3:
            printf("Voce vai pagar R$ %.2f", valor);
            break;
        case 4:
            printf("Voce vai pagar R$ %.2f", valor+((10/100)*valor));
            break;
        default:
            printf("Selecione uma das opções para realizar o pagamento");
            break;
    }   
}
  • In the first case is missing a ")"

  • I fixed it, but it keeps showing 100 in all options. I can’t perform the operation of reducing or increasing the price in the printf function

  • 1

    You are doing whole arithmetic. Try dividing by 100.0 rather than divided by 100

  • 1

    It worked. Thank you very much, but why does it work only I put decimal number in the division?

  • 1

    @Carlos later I answer. Don’t let me forget. I’m busy right now, but I have some answers about floating dots, so maybe this will open your mind. And, yes, it is very confusing for those who are starting, do not be scared. With time gets used

2 answers

-1

You can do as for example (since the values are static) :

case 1:
    aux = (valor - 1) * valor;
    printf ("Voce vai pagar R$ %.2f", aux );
  • 1

    as you yourself commented, the program has syntactic errors, and this is what is triggering the problem described.

-1

You must not be running the same program that compiles.

As noted in the comments, we are missing one ) in the first case -

       printf("Voce vai pagar R$ %.2f", valor-((10/100)*valor));

Open 3 parentheses, and close only 2. This happens in all your other printf: in that condition the program does not compile. You may be changing the source code, but the program that runs is always the last one that could be compiled - in this case it must be some test that only printed "100".

Just pay attention to the error messages of your compiler and your environment to see the syntax errors, as well as the date and time of the executable file (".exe", if it’s windows) you’re running.

Browser other questions tagged

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