Code doesn’t do multiplication. What am I doing wrong?

Asked

Viewed 50 times

3

I’m trying to do the multiplication, but it just doesn’t show up the result of the calculation, which is wrong?

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

void removerNL(char *c) {
    c[strlen(c) - 1] = 0;
}

int main()
{
    int resultado;

    printf("\nCodigo do Locacao (somente numeros): ");
    char scodigoLoca[5];
    fgets(scodigoLoca, 5, stdin);
    removerNL(scodigoLoca);
    int codigoLoca = atoi(scodigoLoca);

    printf("\nCodigo do Cliente (somente numeros): ");
    char scodigoCliente[5];
    fgets(scodigoCliente, 5, stdin);
    removerNL(scodigoCliente);
    int codigoCliente = atoi(scodigoCliente);

    printf("\nValor do automovel: ");
    char svalor[10];
    fgets(svalor, 10, stdin);
    removerNL(svalor);
    int valor = atoi(svalor);

    printf("\nQuantidade de automoveis: ");
    char squant[5];
    fgets(squant, 5, stdin);
    removerNL(squant);
    int quant = atoi(squant);

    resultado = valor*quant;


    printf("Codigo de locacao: %d\n", codigoLoca);
    printf("Codigo do cliente: %d\n", codigoCliente);
    printf("Valor: %d\n", valor);
    printf("Quantidade: %d\n", quant);
    printf("\nValor total da locacao: ", resultado);

}
  • Related (not duplicated): https://answall.com/q/250852/132

  • 4

    You forgot the %d on his last printf.

  • See working: https://ideone.com/wvT9Zv; note that the removal of the NL will return the wrong value if you forget the enter (as it was in this case of automated testing), I would recommend looking if the character in strlen(c) - 1 really is \n

  • 1

    To remove the possible \r and/or \n of the end of a buffer filled in by fgets(), suffice: buffer[ strcspn( buffer, "\r\n" ) ] = 0;

1 answer

0

Instead of using fgets() combined with atoi():

char svalor[10];
fgets(svalor, 10, stdin);
removerNL(svalor);
int valor = atoi(svalor);

You can simply use scanf():

int valor;
scanf("%d", &valor);

Follow an improved version of your code using scanf():

#include <stdio.h>

int main( void )
{
    int resultado, codigoLoca, codigoCliente, valor, quant;

    printf("\nCodigo do Locacao (somente numeros): ");
    scanf("%d", &codigoLoca);

    printf("\nCodigo do Cliente (somente numeros): ");
    scanf("%d", &codigoCliente);

    printf("\nValor do automovel: ");
    scanf("%d", &valor);

    printf("\nQuantidade de automoveis: ");
    scanf("%d", &quant);

    resultado = valor * quant;

    printf("Codigo de locacao: %d\n", codigoLoca);
    printf("Codigo do cliente: %d\n", codigoCliente);
    printf("Valor: %d\n", valor);
    printf("Quantidade: %d\n", quant);
    printf("\nValor total da locacao: %d\n", resultado);

    return 0;
}

However, if the intention is even to use the pair of functions fgets()/atoi(), I suggest using a buffer unique. Note that atoi() makes the conversion of buffer for an integer value without the need to remove the new line of the end.

Follow another improved version of your code using fgets() and atoi():

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

#define MAX_BUF   (10)

int main()
{
    int resultado, codigoLoca, codigoCliente, valor, quant;
    char buf[MAX_BUF];

    printf("\nCodigo do Locacao (somente numeros): ");
    fgets(buf, MAX_BUF, stdin);
    codigoLoca = atoi(buf);

    printf("\nCodigo do Cliente (somente numeros): ");
    fgets(buf, MAX_BUF, stdin );
    codigoCliente = atoi(buf);

    printf("\nValor do automovel: ");
    fgets( buf, MAX_BUF, stdin );
    valor = atoi(buf);

    printf("\nQuantidade de automoveis: ");
    fgets( buf, MAX_BUF, stdin );
    quant = atoi(buf);

    resultado = valor * quant;

    printf("Codigo de locacao: %d\n", codigoLoca);
    printf("Codigo do cliente: %d\n", codigoCliente);
    printf("Valor: %d\n", valor);
    printf("Quantidade: %d\n", quant);
    printf("\nValor total da locacao: %d\n", resultado);

    return 0;
}

Browser other questions tagged

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