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
– Victor Stafusa
You forgot the
%d
on his lastprintf
.– Victor Stafusa
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
– Jefferson Quesado
To remove the possible
\r
and/or\n
of the end of abuffer
filled in byfgets()
, suffice:buffer[ strcspn( buffer, "\r\n" ) ] = 0;
– Lacobus