# Sum of checks lot, total value and validation

Posted on

#### Question :

Write a program in C to validate a batch of checks. The program should initially request the sum of the lot and the number of checks. Then read the value of each check by calculating the total sum. After typing all checks, the program should print the following messages: LOT OK if the reported sum is equal to the calculated sum. Negative difference if the calculated sum is less than the one reported. Positive difference if the sum calculated is greater than that reported. Note: The difference value should be printed (if any).

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

void main()
{
setlocale(LC_ALL, "Portuguese");

int somaLote, aux;
float somaCheques, somaTotal;

printf("Digite a quantidade de cheques: ");
scanf("%d", somaLote);

for(aux=0; aux<=somaLote; aux++)
{
printf("Insira o valor do cheque: ");
scanf("%f", &somaCheques);
somaTotal = somaTotal + somaCheques;
}
printf("Soma total dos cheques: %d", somaLote);
printf("Valor total dos cheques: %f", somaTotal);
}
``````

The compiler hangs and gets error.

It has several errors and is incomplete:

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

int  main() {
setlocale(LC_ALL, "Portuguese");

int qtde, aux;
float valorCheque, valorTotal = 0, totalLote;

printf("Digite a quantidade de cheques: ");
scanf("%d", &qtde);

printf("Digite a soma do lote: ");
scanf("%f", &totalLote);

for(aux = 0; aux < qtde; aux++) {
printf("Insira o valor do cheque: ");
scanf("%f", &valorCheque);
valorTotal += valorCheque;
}
printf("Soma total dos cheques: %f", totalLote);
printf("Valor total dos cheques: %f", valorTotal);
if (totalLote == valorTotal) {
printf("Lote ok");
} else if (totalLote > valorTotal) {
printf("Diferença negativa");
} else {
printf("Diferença positiva");
}
return 0;
}
``````

I’ve changed the variable names to make it clearer what each one does. Probably the confusion has begun there. The code used a variable that was to control the amount of checks as the control of the lot total. Who was even asked as the statement says. And this caused a lot of mistakes.

The `scanf` was passing a value as an argument, when in fact it should pass a memory address. Then the `&` operator was missing.

Obviously checking if the batch is ok or not done.

I will not even mention `float` can not be used for monetary values because this is just an exercise.

The error is here:

``````printf("Digite a quantidade de cheques: ");
scanf("%d", somaLote);
``````

where the correct one would be:

``````printf("Digite a quantidade de cheques: ");
scanf("%d", &somaLote);
``````

To store something, it is mandatory to use & before the name of the variable.

Posted in qnaTagged