Passing string from a function into another function

0

this program functioned as taking the average of each 3 test and categorize it to "low", "medium", "high". the "input" function is used to collect data from a user . "deter" function was to calculate the average and to categorize it. "display" function was to show all the results. In this program I can pass float type data but why I can't pass with string type data. Can someone help me pls?.

#include <stdio.h>
void input ( float*, float*, float* );
void deter (float , float ,float ,float*,char* cate);
void display (float, float, float, float, char);


int main ()
{
    float t1_in, t2_in, t3_in, average;
    char category[10];

    input (&t1_in,&t2_in,&t3_in);
    deter(t1_in,t2_in,t3_in,&average,&category);
    display(t1_in, t2_in, t3_in, average, category);
}

void input ( float* t1, float* t2, float* t3)
{
    printf ("Enter Test 1 >> ");
    scanf ("%f",t1);

    printf ("Enter Test 2 >> ");
    scanf ("%f",t2);

    printf ("Enter Test 3 >> ");
    scanf ("%f",t3);
}


void deter (float tt1_det, float tt2_det,float tt3_det,float* avg,char* cate)
{
    *avg = (tt1_det+tt2_det+tt3_det)/3.0;

    if(*avg<2.0)
    {
        *cate="slow";
    }
    else if(*avg>=2.0 && *avg<=4.0)
    {
        *cate="Medium";
    }
    else if( *avg>4.0)
    {
        *cate="High";
    }
}

void display (float tt1, float tt2,float tt3,float avrg,char ct)
{
    printf ("\nTest 1 is %.2f ",tt1);
    printf ("\nTest 2 is %.2f ",tt2);
    printf ("\nTest 3 is %.2f ",tt3);
    printf ("\nAverage >> %.2f",avrg);
    printf("\nCategory >> %10s",ct);
}

here is my out put

Enter Test 1 >> 1

Enter Test 2 >> 2

Enter Test 3 >> 3

Test 1 is 1.00

Test 2 is 2.00

Test 3 is 3.00

Average >> 2.00

Process returned -1073741819 (0xC0000005) execution time : 1.478 s

Press any key to continue.

c
string
if-statement
func
asked on Stack Overflow Jan 31, 2021 by brian • edited Jan 31, 2021 by brian

1 Answer

0

You need to fix several things in your code:

  1. Change the last param into char*:

    void display(float, float, float, float, char*);
    
  2. Remove & from category:

    deter(t1_in, t2_in, t3_in, &average, category);
    
  3. Use strcpy() function to copy strings into the pointer:

    if (*avg < 2.0)
        strcpy(cate, "Slow");
    else if (*avg >= 2.0 && *avg <= 4.0)
        strcpy(cate, "Medium");
    else if (*avg > 4.0)
        strcpy(cate, "High");
    
  4. Change param char ct into pointer char *ct:

    void display(float tt1, float tt2, float tt3, float avrg, char *ct) {
        .
        .
        printf("\nCategory >> %10s", ct);
    }
    

And you're done.

answered on Stack Overflow Jan 31, 2021 by Rohan Bari • edited Jan 31, 2021 by Rohan Bari

User contributions licensed under CC BY-SA 3.0