Grossu Iulian
Grossu Iulian

Reputation: 275

Using enums and switch in functions (C18 Compiler)

I recently started rewriting a program to make it more easy to understand and I started using enums for describing the different states my program can be in.

byte VoltageLimit(byte progState, word voltage)
{
    switch(progState){
        case Charge:
        case DiagCharge1:
        case DiagCharge2:
            if(voltage>ConstUMax)
                {return 1;}                  
            else 
                {return 0;}
        case Diagnose:
            if(voltage<ConstUMin)
                {return 1;}
            else 
                {return 0;}
        default:
            return 0;
    } 
}

Here is the enum:

enum EnumProgramState
{
    Idle,
    Charging,
    Done,
    DiagCharging1,
    DiagBattery,
    DiagCharging2,
    DiagDone,
    Error
}

I thought I could get away with passing it as byte to the function since the values are actually integers, but I am not sure if this causes the problem or something else, I have been modifying this function for ages now.

The error I get when compiling (2 errors on the same line), the error appears always on the first line I write in a specific case. I have no idea what it is trying to tell me .

main.c:159:Error [1113] integer constant expected for case label value
main.c:159:Error [1113] integer constant expected for case label value

The question: What am I doing wrong, how can I get my code working?

Upvotes: 2

Views: 2010

Answers (2)

Dhruv Acharya
Dhruv Acharya

Reputation: 19

You can also give this a try:

typedef enum
{
    Idle,
    Charging,
    Done,
    DiagCharging1,
    DiagBattery,
    DiagCharging2,
    DiagDone,
    Error
} ProgState;

byte VoltageLimit(ProgState xMyProgState, word voltage)
{
    switch(xMyProgState){
        case Charge:
        case DiagCharge1:
        case DiagCharge2:
            if(voltage>ConstUMax)
                {return 1;}                  
            else 
                {return 0;}
        case Diagnose:
            if(voltage<ConstUMin)
                {return 1;}
            else 
                {return 0;}
        /*
        default:
            return 0;
        */
    } 
}

It will give you specific errors like this:

Build error: 'Charge' undeclared (first use in this function)

and If you comment the default case then you can keep track of that whether you have implemented each case statement or not.

Upvotes: 1

jolati
jolati

Reputation: 700

Unsigned chars will work as expected. You have several typos or undefined labels:

case Charge: but the enum is Charging

case DiagCharge1: but the enum is DiagCharging1, etc.

Upvotes: 3

Related Questions