Doubts if else returning 0

1

I'm trying to do the following exercise:

  

10) A fruit bowl is selling fruits with the following price list:

    Até 5 Kg        Acima de 5 Kg Morango R$ 2,50 por Kg  R$ 2,20 por Kg Maçã    R$ 1,80 por Kg  R$ 1,50 por Kg
     

If the customer buys more than 8 kg in fruit or the total value of the purchase   exceed R $ 25, you will still receive a 10% discount on this   total. Write an algorithm to read the amount (in kg) of strawberries   and the amount (in kg) of purchased apples and write the value to be   paid by the customer.

I wrote the first part of the code but is returning $ 0.00 when testing, What error did I make? how can I solve it?

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

main()
{
    float qmor,qmac,totalfru,valormor,valormac,valortotal;
    printf ("Quantidade de Morangos(Kg):");
    scanf ("%f",&qmor);
    printf ("Quantidade de Macas(Kg):");
    scanf ("%f",&qmac);
    totalfru=qmor+qmac;
    valortotal=valormor+valormac;
    if (qmor<=5)
    {
        valormor=2.50*qmor;       
    }
    else
    {
        valormor=2.20*qmor;
    }
    if (qmac<=5)
    {
        valormac=1.80*qmac;
    }
    else
    {
        valormac=1.50*qmac;
    }
    printf ("Valor Total: R$%.2f",valortotal);

}
    
asked by anonymous 03.09.2018 / 01:07

2 answers

2
valortotal=valormor+valormac;

valormor and valormac is only initialized with a value below, you should then change that line valortotal=valormor+valormac; to the end.

    if (qmor<=5)
        valormor=2.50*qmor;

    else
        valormor=2.20*qmor;

    if (qmac<=5)
        valormac=1.80*qmac;

    else
        valormac=1.50*qmac;

    valortotal=valormor+valormac;
    
03.09.2018 / 01:13
1

The Total Value must perform the operation after the variables plusvalue andvalue have been treated. Since they start with a value of 0 at the moment of the account they continue with this value, since no operation was done with them.

Basically what is happening is:

valuemore and valuemac begins storing the value 0, then the variable totalvalue receives the value of the result of the sum of these two variables. In the sequence, the values and values are treated and receive different values, but the total value continues with the same value of the beginning, since only one copy of the sum of these two variables was stored, with a value of 0 remaining in it.     

03.09.2018 / 01:23