reset problem in C code

Asked

Viewed 153 times

-3

I would like help with the code I’m having a problem that follows in the image:inserir a descrição da imagem aqui

I’m using this code:

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

int main ()
{
    float CP, LP, PP, LA, AA, AT, AAZ, PL, PC, FP;
    int QA;
    printf ("\nDigite os dados da picina");
    printf ("\nComprimento, Largura, Profundidade: \n");
    {
        scanf("%f,%f, %f", &CP, &LA, &AA);
        PL = 2*CP*PP;
        PC = 2*LP*PP;
        FP = CP*LP;
        AT = PL+PC+FP;
        AAZ = LA*AA;
        QA = ((AT/AAZ)*1.05);
    }
    printf("\nA quantidade de azulejos para o revestimento da piscina e: %f", QA);
    system("PAUSE");
}
  • no matter what number I put it reset and 0.0000 as a result and also skips the others I should put in the program

  • What is the value of the variable PP?

2 answers

1

Mete

    if (scanf("%f%f%f", &CP, &LA, &AA) != 3) /* erro */;

Instead of

    scanf("%f,%f, %f", &CP, &LA, &AA);

The conversion "%f, %f, %f" means to read an optional float preceded by spaces, a comma, another float (with optional space before), another comma, optional space, and another float with optional space.

With the input "10" the variables LA and AA are not allocated and the programme makes wrong accounts.


Even better and input with fgets() and then sscanf() to assign value to variables

printf("Valores? ");
if (!fgets(tmp, sizeof tmp, stdin)) /* error */;
if (sscanf(tmp, "%f%f%f", &CP, &LA, &AA) != 3) /* error */;
  • the error still persists, I managed to do in another code and did not give error, I think the error is not in these parts.

  • @matheusferreira You are not initialized some variables. What pmg answered is right, but without some values the result will not change.

  • yes I arranged the part "%f, %f, %f", I’m trying to find a way to start them.

0


In addition to the problem presented by @pmg, you are also not initiating variables PP and LP, which caused the program to run at unknown values for these variables. Ironically, these values must have been extremely low when you ran your code (values like 5.88639522e-039 and 1.12103877e-044), which in practical terms is zero.

Similarly, as the variables were not initialized, such values could have been extremely high, and consequently the result would be another.

With this (recital PP and LP null), PL, PC and FP were also zeroed because they multiply values PP and LP.

PL = 2*CP*PP;
PC = 2*LP*PP;
FP = CP*LP;

And with that, AT will also be zero.

AT = PL+PC+FP;

And finally, QA also.

QA = ((AT/AAZ)*1.05);

With this, independent of the input value, the result will be zero (because QA is zero):

printf("\nA quantidade de azulejos para o revestimento da piscina e: %f", QA);
  • Uninitialized values are uninitialized values, they can be anything. Only global values would be zero.

  • @Actually, I just checked here. They’re not zeroed, but ironically, they assumed very low values, so they showed zero in output when I tested them. But, similarly, after rotating more often, sometimes these values can be absurdly high when not initialized. I will correct.

Browser other questions tagged

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