Checkbook batch sum, total value and validation

Asked

Viewed 468 times

4

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

Follow the code done so far.

#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);
}

Compiler crashes and keeps giving error.

2 answers

4


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");
}

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

I changed the names of the variables to make it clearer what each one does. Probably the confusion has already started there. The code used a variable that was to control the amount of checks as controlling the total of the lot. Who was even asked as stated. And this generated a series of errors.

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

Obviously the check if the batch is ok nor was made.

Nor will I mention that float cannot be used for monetary values because this is just an exercise.

  • 1

    I don’t think it’s right to give the answer to the algorithm, since it’s an exercise, where the purpose is to get it to train the language.

  • The question begs this. And he didn’t order ready, he asked us to solve his logic problem. There are several errors in the code, solving one does not help anything.

  • 1

    Ms the rest is part of the question, not only this sentence. If you take to the letter only what is with question as question, then the answer is "Yes".

  • 1

    What was requested was the solution of the "compiler lock and keep giving error", what was generated by the scanf without the &, assemble all the logic and do the exercise I think runs a little. Whereas what was asked was that we "scan" the code behind the bug that causes the compiler to crash.

  • 1

    Then he decides which answer is best for him.

3

The error is here:

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

where the correct would be:

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

To store something is required to use & before the variable name.

  • What a silly mistake I made there ein hahaha, thank you man!

Browser other questions tagged

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