Home > Software engineering >  How do I write a correct function in C?
How do I write a correct function in C?

Time:11-30

So this is in german, but I have to write a function in C but it does not work out. Can you tell me where my mistake is?

The task is the one Iam writing down now.

Write a function "time_converter( )" which converts the time from one unit to another. The function is passed the case (i.e. which conversion is to be performed) and the number to be converted in exactly this order. The case is given a number between 1 and 6 and the number to be converted is given as a natural number. The result of the conversion is to be returned to 2 digits exactly at the end of the function. The following cases are to be implemented:

  1. from second to minute

  2. from second to hour

  3. from minute to hour

  4. from hour to minute

  5. from hour to second

  6. from minute to second

     int zeit_umrechner(float z, char e,  char ee ){
     if (e==sekunde && ee==minute)
     {return z/60;}
    
     if (e==sekunde && ee== stunde)
     {return z/3600;}
    
     if (e==minute && ee== stunde)
     {return z/60;}
    
     if (e==stunde && ee== minute)
     {return z*60;}
    
     if (e==stunde && ee== sekunde)
     {return z*3600;}
    
     if (e==minute && ee== sekunde)
     {return z*60;}
    
     return 0;
    

    }

I tried, but it does not work out.

CodePudding user response:

There seems to be bits missing from the example, but taking into account of what you appear to be attempting to create a time conversion function, and noting the comments that regarding your function signature that should not have a definition of 'stdin' in it, I filled in some missing pieces to devise a code snippet that should provide the functionality that you are after.

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

char sekunde = 's';     /* Not in your example - but they would need to be defined as globale somewhere in order to work in the function */
char minute  = 'm';
char stunde  = 'h';

int zeit_umrechner(float z, char e, char ee ){

    if (e==sekunde && ee==minute)
    {return z/60;}

    if (e==sekunde && ee== stunde)
    {return z/3600;}

    if (e==minute && ee== stunde)
    {return z/60;}

    if (e==stunde && ee== minute)
    {return z*60;}

    if (e==stunde && ee== sekunde)
    {return z*3600;}

    if (e==minute && ee== sekunde)
    {return z*60;}

    return 0;
}

int main()
{
    float entry;
    char from_unit, to_unit;

    printf("Enter time value: ");
    scanf("%f", &entry);

    printf("Enter current unit s/m/h: ");
    scanf(" %c", &from_unit);

    printf("Enter unit to convert to: ");
    scanf(" %c", &to_unit);

    printf("Time conversion is: %d\n", zeit_umrechner(entry, from_unit, to_unit));

    return 0;
}

The bits to note here.

  • Since the character values in your function are not defined within your function, they would need to be defined as global variables or within "#define" statements, which was added to the snippet.
  • The function signature was cleaned up to provide the most likely scenario for parameter passing.
  • A sample setup of input and a call was added to the main function to test out the function.

With that, following is some sample output.

@Dev:~/C_Programs/Console/TimeConvert/bin/Release$ ./TimeConvert 
Enter time value: 3600
Enter current unit s/m/h: s
Enter unit to convert to: h
Time conversion is: 1

This is only one possible ways to do this, but give it a try and see if it meets the spirit of your project.

CodePudding user response:

This is a function to convert time from one unit to another. For example, seconds to minutes.

It takes the time as a float, for example 1.23, and then the from/to units (e and ee) as strings. The string type in C is char *. Since you don't plan on modifying the strings, we'll use const char * to tell the compiler they are constant and to warn us if we change them.

Furthermore, sekunde and minute are also strings. Without quotes they are considered variables. They should be in quotes, "sekunde" and "minute".

You can't compare strings with ==. That only works on numbers. You need to use strcmp ("STRing CoMPare"). This returns 0 on match.

Finally, I've taken the liberty of using a more standard style.

int zeit_umrechner(float z, const char *e, const char *ee ) {
    if (strcmp(e, "sekunde") == 0 && strcmp(ee, "minute") == 0) {
        return z/60;
    }
    if( strcmp(e, "sekunde") == 0 && strcmp(ee, "stunde") == 0 ) {
        return z/3600;
    }

    //and so on
}

You can make this a bit more compact by using nested if statements.

int zeit_umrechner(float z, const char *e, const char *ee ) {
    if (strcmp(e, "sekunde") == 0) {
        if(strcmp(ee, "minute") == 0) {
            return z/60;
        }
        else if(strcmp(ee, "stunde") == 0) {
            return z/3600;
        }
    }

    // and so on
}

Note that because you're returning an int you will only get whole numbers back. For example, zeit_umrechner(90, "sekunde", "minute") will return 1 because 90/60 is 1.5 which is truncated to 1.

  •  Tags:  
  • c
  • Related