1
The goal of the program I’m creating is to receive the distance that 15 cars (simulated with 3 to speed up the test process) traveled during the day. Distance can be reported in kilometers, meters or miles. The program has to return the total distance in kilometers.
That is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
float testeMedida(char medida,float total) {
if (strcmp(medida,"km") == 0) {
return total;
} else if (strcmp(medida,"mt") == 0) {
return total/1000;
} else if (strcmp(medida,"mi") == 0) {
return total*1.60934;
}
}
void main() {
float distancia[3];
float resultado, totalConveter;
int i;
char medida[5];
for (i = 0; i < 3; i++) {
printf("Digite a distancia percorrida pelo carro %d: ",i);
scanf("%f",&distancia[i]);
totalConveter = totalConveter + distancia[i];
}
printf("\nQual a unidade utilizada?\n");
printf("km = quilometros\n");
printf("mt = metros\n");
printf("mi = milhas\n");
printf("Unidade utilizada: ");
scanf("%s",&medida);
resultado = testeMedida(medida,totalConveter);
printf("\nA distancia total percorrida pelos carros são de %.2f quilometros.",resultado);
getchar();
}
But when executing the code, the program does not return the total distance of the last printf
. It closes after informing the unit of measure.
What can it be?
Thanks for your attention. :)
You’re passing a string (a vector/string, i.e., type
char[]
) for a parameter that you defined should be a single character, i.e., typechar
.– Gustavo Sampaio
Thank you very much Gustavo Sampaio. I was breaking my head and until now I hadn’t noticed it. I’m starting in the area of programming.
– EuNerd
Problem solved.
– EuNerd