Variable type float does not receive valuation

Asked

Viewed 295 times

-4

Follows an excerpt from the code:

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

#define MVAIN  0.8592;
#define MVAOUT 1.0282;
#define ICMS 0.29;

float ProdValue = 0, MVA = 0, ResultValue1 = 0, ResultValue2 = 0, ResultValue3 = 0;

int main(){
    printf("Indique o valor dos produtos da nfe que contenham ST \n");
    scanf ("%f", &ProdValue);   

    printf ("O valor dos produtos é:  %f \n ", &ProdValue);


    printf ("Informe se o MVA é fora ou dentro do estado \n");
    scanf ("%f", &MVA) ;


if(MVA == 1) {
    ResultValue1 =  ProdValue*MVAIN; //926,23
    ResultValue2 =  ResultValue1+ProdValue; //2004,25

    ResultValue3 =  ResultValue2*ICMS; //581,23
    ResultValue2 =  ResultValue1-ResultValue2; //345,00

    printf ("O valor a recolher é %0.2f \n", ResultValue2);


    } if (MVA == 2) {
        ResultValue1 =  ProdValue*MVAOUT; //926,23
        ResultValue2 =  ResultValue1+ProdValue; //2004,25

        ResultValue3 =  ResultValue2*ICMS; //581,23
        ResultValue2 =  ResultValue1-ResultValue2; //345,00


        printf ("O valor a recolher é %0.2f \n", ResultValue2);     
  • 1

    What is the problem? Note that monetary value cannot be float but I don’t think this is the problem. http://answall.com/q/38138/101. This code neither compiles. What value should you enter to test?

  • It even compiles, actually missing the code snippet, note that it ends in a "printf", I have his exe here, however the float values do not receive the stipulated value, for example 1078.02. When I try to assign this value he does not receive. I tried to print on the screen the values however remain zeroed even after the assignments, this for attribution by input as direct.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

In a good compiler the code does not even compile. If compile is worse because the code is full d errors. I will not even mention the fact of store monetary value with float be a mistake. The code is confusing, too complicated and has logic error. I improved, but it is not good yet. It was not very clear the error, after I fix the errors that prevented the compilation no error happened.

#include <stdio.h>

#define MVAIN  0.8592f
#define MVAOUT 1.0282f
#define ICMS 0.29f

int main(){
    printf("Indique o valor dos produtos da nfe que contenham ST\n");
    float ProdValue = 0;
    scanf("%f", &ProdValue);   
    printf("O valor dos produtos é: %0.2f\n", ProdValue);
    printf("Informe se o MVA é fora ou dentro do estado\n");
    int MVA = 0;
    scanf("%d", &MVA);
    float ValorMVA = ProdValue * (MVA == 2 ? MVAOUT : MVAIN);
    printf ("O valor do MVA é %0.2f\n", ValorMVA);
    float ValorComMVA = ProdValue + ValorMVA;
    printf ("O valor total é %0.2f\n", ValorComMVA);
    float ValorIcms = ValorComMVA * ICMS;
    printf ("O valor do ICMS é %0.2f\n", ValorIcms);
    float Diferenca = ValorMVA - ValorIcms;
    printf ("O valor a recolher é %0.2f\n", Diferenca);
}

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

  • Good evening, I found out where the error was, I’m using DEVC++, I still don’t know how to define if a compiler is good or bad because I have little practice in this, however the error was really in the Float variables, when changing them to double I was able to store them manually. However it continued in the same way without saving the entries by scanf, as it was using a double variable I would have to use "%lf". Now this is correct the calculation. Thank you and sorry to take up your time, I didn’t even use your code.

  • Dev-C++ is a bomb, I already knew I was using it. Switch to double is not to solve the problem in fact, in fact monetary value can not use double.

Browser other questions tagged

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