Home > OS >  Understanding how the switch statement works in C - am I missing something?
Understanding how the switch statement works in C - am I missing something?

Time:09-23

It gives no error but the switch statement always goes to the default case. I didn't paste the functions for the sake of concision.

I was advised to break my programs into subroutines, but is structured programming always better? Even if it takes more line of code and longer to execute?

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

#ifdef M_PI
#define PI M_PI
#else
#define PI 3.14159265358979323846264338327950288420
#endif // M_PI

int getData(int op);
float degreesToRadians(float angle);
float sine(float degrees);
float cosine(float degrees);
float tangent(float degrees);
float sineInverse(float degrees);
float cosineInverse(float degrees);
float tangentInverse(float degrees);

int main(void)
{
    float degrees;
    int op;

    getData(op);

    printf("Enter the angle in degrees: ");
    scanf("%f", &degrees);

    degreesToRadians(degrees);

    switch(op)
    {
    case 1:
        sine(degrees);
        break;

    case 2:
        cosine(degrees);
        break;

    case 3:
        tangent(degrees);
        break;

    case 4:
        sineInverse(degrees);
        break;

    case 5:
        cosineInverse(degrees);
        break;

    case 6:
        tangentInverse(degrees);
        break;

    default:
        printf("You entered an invalid option!");
        break;
    }
    return 0;
}

CodePudding user response:

Your understanding of the switch statement is correct. The switch expression, op here, is evaluated once, and compared with the constant expressions 1: 2: etc, and you have a default catch-all for when there is no match.

However, if you always end up in the default clause, this would be indicative of your case expressions never matching the op expression's value. Note that you have written code with "undefined behavior" - because op is not defined anywhere. (This needs to be fixed.)

As far as I can tell, you are trying to retrieve a value from getData() by passing it an operand op. However, getData() is declared as returning an int parameter but you don't use any returned value. The variable op will not have its value changed after the call to getData() because you are passing-by-value. (If you had passed by reference, for example a pointer, the getData() function could change the value of op for you.) Unfortunately, you haven't included getData() in the example code so we can't tell what its purpose is.

Similarly, you are passing the degrees variable to a conversion function but your are not using the converted value. As you can see, all of the float functions declared at the top of the file will return float values. You should use these returned values.

Suggestion: Use the value returned by getData() and the floating point functions:

int data;
int op = ...;  // op must be defined!
float radians;
float trig_value;

data = getData(op);

radians = degreesToRadians(degrees);

switch (data) {
    ...
    trig_value = sine(radians);
}
  • Related