Switch case程序不提供输出

时间:2014-10-06 23:04:41

标签: c

#include<stdio.h>
int main()
{

int choice;
printf("Enter 1 for Programmers Name and ID\n");
printf("Enter 2 to Perform Integer Operation\n");
printf("Enter 3 to Perform Floating Point Operation\n");
scanf("%d", &choice);
system("CLS");

if (choice == 1)
    printf("Connor \n000000000\n");

else if (choice == 2)
{
    char c;
    int num1, num2;
    printf("Enter operator:");
    scanf("%c", &c);
    getchar();
    printf("Enter two integer's :");
    scanf("%d %d", &num1, &num2);

    switch (c)
    {
    case '+':
        printf("%d + %d = %d", num1, num2, num1 + num2);
        break;
    case '-':
        printf("%d - %d = %d", num1, num2, num1 - num2);
        break;
    case '*':
        printf("%d * %d = %d", num1, num2, num1*num2);
        break;
    case '/':
        printf("%d / %d = %d", num1, num2, num1 / num2);
        break;

    default:
        printf("The value of c = '%c'\n");
        system("pause");
        return(0);
    }
}
else if (choice == 3)
    printf("Enter two \n");


system("pause");
return(0);
}

我需要一些帮助来解决这个代码的操作部分的一个小问题.......一切正常,因为可以放入运算符和整数但我没有得到交换机的输出

2 个答案:

答案 0 :(得分:0)

这绝对有用

    #include<stdio.h>
int main()
{

int choice;
printf("Enter 1 for Programmers Name and ID\n");
printf("Enter 2 to Perform Integer Operation\n");
printf("Enter 3 to Perform Floating Point Operation\n");
scanf("%d", &choice);
getchar();
if (choice == 1)

    printf("Connor \n000000000\n");


else if (choice == 2)


{
    char c;
    int num1, num2;
    printf("Enter operator:");
    scanf("%c", &c);
    printf("Enter two integer's :");
    scanf("%d %d", &num1, &num2);
    switch (c)
    {

    case '+':
        printf("%d + %d = %d", num1, num2, num1 + num2);
        break;
    case '-':
        printf("%d - %d = %d", num1, num2, num1 - num2);
        break;
    case '*':
        printf("%d * %d = %d", num1, num2, num1*num2);
        break;
    case '/':
        printf("%d / %d = %d", num1, num2, num1 / num2);
        break;

    default:
        printf("The value of c = '%c'\n" , c);
        return(0);
    }

}
else if (choice == 3)
    printf("Enter two \n");

}

答案 1 :(得分:-1)

  1. 变化:

    scanf("%c", &c);
    getchar();
    

    为:

    scanf(" %c", &c);
    
  2. 为每个人添加\n

    printf("%d + %d = %d\n", num1, num2, num1 + num2);
                        ^^
    
  3. 当您告诉char打印一个时,实际提供printf()

    printf("The value of c = '%c'\n", c);
                                    ^^^
    
  4. 它应该适合你。修改后的代码,删除了所有system()废话:

    #include <stdio.h>
    
    int main(void)
    {
        int choice;
    
        printf("Enter 1 for Programmers Name and ID\n");
        printf("Enter 2 to Perform Integer Operation\n");
        printf("Enter 3 to Perform Floating Point Operation\n");
        scanf("%d", &choice);
    
        if ( choice == 1 ) {
            printf("Connor \n000000000\n");
        }
        else if ( choice == 2 ) {
            char c;
            int num1, num2;
    
            printf("Enter operator:");
            scanf(" %c", &c);
    
            printf("Enter two integers :");
            scanf("%d %d", &num1, &num2);
    
            switch ( c ) {
    
            case '+':
                printf("%d + %d = %d\n", num1, num2, num1 + num2);
                break;
            case '-':
                printf("%d - %d = %d\n", num1, num2, num1 - num2);
                break;
            case '*':
                printf("%d * %d = %d\n", num1, num2, num1 * num2);
                break;
            case '/':
                printf("%d / %d = %d\n", num1, num2, num1 / num2);
                break;
            default:
                printf("The value of c = '%c'\n", c);
                break;
            }
        } else if ( choice == 3 ) {
            printf("Enter two \n");
        }
        else {
            printf("Invalid choice.\n");
        }
    
        return 0;
    }
    

    带样本输出:

    paul@thoth:~/src/sandbox$ ./cal
    Enter 1 for Programmers Name and ID
    Enter 2 to Perform Integer Operation
    Enter 3 to Perform Floating Point Operation
    2
    Enter operator:*
    Enter two integers :4 6
    4 * 6 = 24
    paul@thoth:~/src/sandbox$