I can’t find the reason why my code doesn’t calculate correctly

Asked

Viewed 52 times

1

I’m starting programming now and to learn new things I’m trying to make a C code that calculates whether the user receives more or less than 3 minimum wages. However when doing the sum of the last 4 user salaries the result is inconsistent with the entered values.

Here is the code

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

int main()
{
    int A = 998; //Salario minimo
    float X, Y, Z, K; //Armazena os 4 ultimos salarios
    int C = A * 3; //Calcula quanto equivale 3 salarios minimos
    float D = X+Y+Z+K; //Soma os 4 ultimos salarios

    printf("Digite seus 4 ultimos salarios, 1º");
    scanf("%f", &X);

    printf("2º");
    scanf("%f", &Y);

    printf("3º");
    scanf("%f", &Z);

    printf("4º");
    scanf("%f", &K);

    printf("A soma dos salarios é: %f", D);

    return 0;
}

Imagem da execução do código

I hope you can help me.

  • 1

    You calculated the value of D before reading the values of X, Y, Z and K?

  • I think that’s it, getting it now that I put the variable to calculate the value of D after reading the values. Thank you.

1 answer

0


Calculate the value of C after capturing the value of X, Y, Z and K, see below:

#include <stdlib.h>
#include <math.h>

int main()
{
    int A = 998; //Salario minimo
    float X, Y, Z, K; //Armazena os 4 ultimos salarios
    int C = A * 3; //Calcula quanto equivale 3 salarios minimos

    printf("Digite seus 4 ultimos salarios, 1º");
    scanf("%f", &X);

    printf("2º");
    scanf("%f", &Y);

    printf("3º");
    scanf("%f", &Z);

    printf("4º");
    scanf("%f", &K);

    float D = X+Y+Z+K; //Soma os 4 ultimos salarios

    printf("A soma dos salarios é: %f", D);

    return 0;
}

Browser other questions tagged

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