Subroutine in C does not execute

Asked

Viewed 56 times

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. :)

  • 1

    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., type char.

  • 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.

  • Problem solved.

1 answer

1


I ran his program through Codeblocks and he pointed out a series of warnings that needed to be addressed. Let’s list 1 to 1 so you can fix your code and run your program:

  1. |6|Warning: Passing argument 1 of ːstrcmp' makes Pointer from integer without a cast [-Wint-Conversion]| ----> The warning here is to let you know that on line 6 of your code, the strcmp function is receiving a parameter that is not a char pointer, so this function will not perform correctly. Your problem can be solved by modifying the tested functionMedida() to receive a char pointer, thus staying as follows:

    float testeMedida(char medida[5],float total) { /* corrigido tambem o parametro de entrada
     char medida que precisava do complemento com o tamanho da char. */ 
     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;
     } else { 
     return -1;
     } 
    } /* Note que acrescentei um else a mais para que a função retorne -1
       caso nenhuma das hipóteses de cima sejam atendidas. Se não acrescentasse, não haveria
       retorno em algum momento e o código apresentaria erro.
    

2 Warning: Return type of ' main' is not ' int' [-Wmain]: Following the good practices in C and pointing out that in C++ void main() it is forbidden, for kindness exchange void main() for int main(), so the program will return the value 0 for Voce if everything has gone well, and -1 if there is any error.

3 Warning: control Reaches end of non-void Function [-Wreturn-type]: It is a compiler warning that informs the need for a standard return to the tested function Measure(). This warning fades when you put the most equal Else I did up there.

4 |33|Warning: format ː%s' expects argument of type ːchar ', but argument 2 has type ľchar ()[5]' [-Wformat=]|: In this warning, it is possible to notice that on line 33 the scanf expects to receive the char type *(char pointer) any, but you have informed &measure, and in the case of a set of char, it is not possible in this case to perform a parameter passage by reference. You must take away & without fear of being happy that the scanf will continue throwing the text typed in the variable and will stop displaying Warning message.

More about & and pointers in: What is the meaning of the operator "&" (and commercial) in the C language?

Run these plasters and your work will be ready.

  • Thank you Alexandre Guerreiro.

Browser other questions tagged

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