Kubilay Doğru
Kubilay Doğru

Reputation: 17

Using Switch statement in another switch in C

So I'm trying to make a program mostly out of curiosity and little bit for my studies! But I came across this problem. When I try to use a switch statement in another one my program goes to default if I choose the case which the switch statement is in.

printf("Please enter the first character of the thing you want to get perimeter and area of off: ");
scanf_s("%c", &holder);

switch (toupper(holder))
{
case 'C':
    printf("Please enter the radius of the circle: ");
    scanf_s("%d", &a);
    if (a <= 0)
        printf("Please enter a positive radius!\n");
    else
    {
        perimeter = 2 * PI * a;
        area = PI * a * a;

        printf("Perimeter of the circle is %.2f\n", perimeter);
        printf("Area of the circle is %.2f\n", area);
    }
    break;
case 'R':
    printf("Please enter the two sides of the rectangle!\n");
    scanf_s("%d %d", &a, &b);

    if (a <= 0 || b <= 0)
        printf("Please enter positive numbers as sides values!\n");
    else
    {
        perimeter = a + a + b + b;
        area = a * b;

        printf("Perimeter of the rectangle is %.2f", perimeter);
        printf("Area of the rectangle is %.2f", area);
    }
    break;
case 'T':
    printf("Please enter a for calculation using 3 sides and b for 2 sides and the angle between them!");
    scanf_s("%c", &e);

    switch (e)
    {
    case 'a':
        printf("Please enter the three sides values!\n");
        scanf_s("%d %d %d", &a, &b, &c);

        if (a <= 0 || b <= 0 || c <= 0 || a >= b + c || b >= a + c || c >= a + b || a < abs(b - c) || b < abs(a - c) || c < abs(a - b))
            printf("Please enter viable side values!\n");
        else
        {
            perimeter = a + b + c;
            d = perimeter / 2;
            area = sqrt(d * (d - a) * (d - b) * (d - c));

            printf("Triangles perimeter is %2.f\n", perimeter);
            printf("Triangles area is %2.f\n", area);
        }
        break;
    case 'b':
        printf("Please enter 2 sides and the degree between them!\n");
        scanf_s("%d %d %d", &a, &b, &c);

        c1 = PI / 180 * c;
        perimeter = (a * a) + (b * b) - (2 * a * b * cos(c1));
        area = (1 / 2) * a * b * sin(c1);

        printf("Triangles perimeter is %2.f\n", perimeter);
        printf("Triangles area is %2.f\n", area);
        break;
    }
default:
    printf("Unknown character!\n");



}

system("pause");

}

Upvotes: 1

Views: 97

Answers (1)

tapananand
tapananand

Reputation: 4872

Your first scanf input you must have given was say T\n (because you gave T followed by the enter key - which is \n on linux - assuming it's linux)

So it's not one but two characters in the stdin buffer currently. Hence, when you scan the character 'e' it actually reads '\n' instead of what the user enters next and hence goes into the default case in the inner switch.

EDIT: Since there is no default case in your inner switch and hence the case of \n is not handled anywhere. This also means no break was executed for the outer switch and hence all subsequent cases including default will be executed, that's why you get Unknown Character

So, if you are sure there will always be a press of the enter key you can do a getchar() before taking the second character.

getchar(); //removes \n from stdin
scanf_s("%c", &e);

Upvotes: 1

Related Questions