jeudi 22 novembre 2018

Return a string from if statement in C

I was trying to let a function to return the correct char value from an array by an if statement but kept getting the following error
returning ‘char *’ from a function with return type ‘char’ makes integer from pointer without a cast

and here is the function

    ...
    char GradeFromPercentage(float x)
    { 
        char y[5][1];
        y[1] == "A";
        y[2] == "B";
        y[3] == "C";
        y[4] == "D";
        y[4] == "F";

        if (x >= 90)
        {
            return y[0];
        }
        else if (x >= 70)
        {
            return y[1];
        }
            else if (x >= 50)
        {
            return y[3];
        }
        else if (x >= 30)
        {
            return y[4];
        }
        else if (x < 30)
        {
            return y[5];
        }
        return 0;
    }
    ...

Aucun commentaire:

Enregistrer un commentaire