切换语句结束

时间:2017-03-13 02:33:01

标签: c if-statement switch-statement

我正在编写一个switch语句,它应该像一个简单的计算器,当我选择case 5时,它应该在单精度和双精度之间切换。但是当我选择案例5时,我得到Calculator now works with double precision.并退出程序。它实际上是在工作之前,直到我添加了案例6,它应该是退出该程序的案例。

int opt; 
float first, second, sum, difference, product, quotient;
double first_d, second_d, sum_d, difference_d, product_d, quotient_d;
char ch;



printf("This program implements a calculator. Options:\n");
printf(" 1 - addition\n 2 - subtraction\n 3 - multiplication\n 4 - division\n");
printf(" 5 - toggle precision\n 6 - exit program");

ch = getchar();
for(;;) {
    printf("Please enter your option:");
    scanf("%d", &opt);
    switch (opt) {
        case 1: 
            printf("Enter first term:");
            scanf("%f", &first);
            printf("Enter second term:");
            scanf("%f", &second);
            sum = first + second;
            printf("The sum is: %f\n", sum);
            break;
        case 2: 
            printf("Enter first term:");
            scanf("%f", &first);
            printf("Enter second term:");
            scanf("%f", &second);
            difference = first - second;    
            printf("the difference is: %f\n", difference);
            break;  
        case 3:
            printf("Enter first term:");
            scanf("%f", &first);
            printf("Enter second term:");
            scanf("%f", &second);
            product = first * second;
            printf("The product is: %f\n", product); 
            break;
        case 4:
            printf("Enter first term:");
            scanf("%f", &first);
            printf("Enter second term:");
            scanf("%f", &second);
            quotient = first/ (float)second;    
            printf("The quotient is: %f\n", quotient);
            if(second == 0){
                printf("Cannot divide by 0!\n");
            }
            break;
        case 5: 
            if(ch != '1' && '2' && '3' && '4' && '6'){ 
                printf("Calculator now works with double precision.\n");
                switch (opt) {
                    case 1: 
                        printf("Enter first term:");
                        scanf("%lf", &first_d);
                        printf("Enter second term:");
                        scanf("%lf", &second_d);
                        sum_d = first_d + second_d;
                        printf("The sum is: %lf\n", sum_d);
                        break;
                    case 2: 
                        printf("Enter first term:");
                        scanf("%lf", &first_d);
                        printf("Enter second term:");
                        scanf("%lf", &second_d);
                        difference_d = first_d - second_d;  
                        printf("the difference is: %lf\n", difference_d);
                        break;  
                    case 3:
                        printf("Enter first term:");
                        scanf("%lf", &first_d);
                        printf("Enter second term:");
                        scanf("%lf", &second_d);
                        product_d = first_d * second_d;
                        printf("The product is: %lf\n", product_d); 
                        break;
                    case 4:
                        printf("Enter first term:");
                        scanf("%lf", &first_d);
                        printf("Enter second term:");
                        scanf("%lf", &second_d);
                        quotient_d = first_d/ (double)second_d;     
                        printf("The quotient is: %lf\n", quotient_d);
                        if(second_d == 0){
                            printf("Cannot divide by 0!\n");
                        } else {
                            printf("Calculator now works with single precision.\n");    
                        }
                        break;
                    }
                }
        case 6: 
            return 0;
        default: 
            printf(" 1 - addition\n 2 - subtraction\n 3 - multiplication\n 4 - division\n");    
            printf(" 5 - toggle precision\n 6 - exit program\n");
            break;
            }
    }
}

1 个答案:

答案 0 :(得分:-1)

正如你原来帖子上的评论所说,你只是忘了休息。

在第五种情况之后,中断会导致代码离开switch语句并继续循环输入。因为你忘记了这个中断,代码在第五种情况下完成并继续进入第六种情况。然后第六种情况退出,这是你的输出最初的原因。

在第五个案例的末尾添加一个break语句,你应该全部准备好了。