Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中的另一个开关中使用Switch语句_C_Switch Statement - Fatal编程技术网

在C中的另一个开关中使用Switch语句

在C中的另一个开关中使用Switch语句,c,switch-statement,C,Switch Statement,所以我想做一个程序,主要是出于好奇,还有一点是为了我的学习!但我遇到了这个问题。当我尝试在另一个switch语句中使用switch语句时,如果我选择switch语句所在的情况,我的程序将进入默认状态 printf("Please enter the first character of the thing you want to get perimeter and area of off: "); scanf_s("%c", &holder); switch (toupper(hold

所以我想做一个程序,主要是出于好奇,还有一点是为了我的学习!但我遇到了这个问题。当我尝试在另一个switch语句中使用switch语句时,如果我选择switch语句所在的情况,我的程序将进入默认状态

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");
printf(“请输入要获取的对象的周长和面积的第一个字符:”;
扫描单位(“%c”和持有人);
开关(顶部(支架))
{
案例“C”:
printf(“请输入圆的半径:”);
scanf_s(“%d”和“&a”);

如果(a您必须给出的第一个scanf输入是说
T\n
(因为您给出了T,然后是回车键,在linux上是
\n
,假设它是linux)

因此,当前stdin缓冲区中不是一个而是两个字符。因此,当您扫描字符
'e'
时,它实际上读取的是
'\n'
,而不是用户接下来输入的内容,因此进入内部开关中的默认情况

编辑:由于内部开关中没有默认大小写,因此不会在任何地方处理
\n
的大小写。这也意味着外部开关没有执行
中断
,因此将执行包括默认值在内的所有后续大小写,这就是为什么会得到
未知字符

因此,如果您确定始终按enter键,则可以在使用第二个字符之前执行
getchar()

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

您的输入是什么?提供完整的代码。同时共享发生这种情况的输入。它是否完全跳过内部
开关
块,还是也转到默认块?如果是这种情况,您正在体验。在内部
开关
后没有
中断
语句。这看起来更像是
扫描f
问题不仅仅是
开关的问题(您的代码缺少外部
开关中
案例“T”
中断
,因此执行将取决于外部
开关的
默认案例。不过,错误消息可能适用于两个开关。)没有aparrent debuggin、DCV.第一输入:t第二输入:a和程序显示“未知字符”,而不是执行案例a。@KubilayDo瀞ru Put
break
在cast
t
之后。在这种情况下,默认值和情况t都会执行。