C,我的 switch 语句在控制台上没有输出

标签 c if-statement switch-statement

我目前正在尝试练习我在大学学到的功能,以备即将到来的考试。我正在尝试使用以下功能; ifswitchscanfprintffor

但是,当我尝试在控制台上执行程序时,一旦我将输入放入变量 n 中,switch 函数就会在控制台上不显示任何输出,并且程序结束。

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(void) {
    int n,i,x,y,z;
    float res;
    printf("Please input x,y,z using (,)\n");
    scanf("%d,%d,%d",&x,&y,&z);
    printf("You inputed the following numbers: x=%d, y=%d, z=%d\n",x,y,z);
    printf("Which of the following equations would you like to run?\n");
    printf("x+y/z ?? (1)\n");
    printf("sqrt(z)+(x^3/y) ?? (2)\n");
    printf("The average number of the three entered ?? (3)\n");
    printf("Print the word 'test' as many times as is the sum of x,y,z ?? (4)\n");
    scanf(" %d",n);
    switch(n)
    {
    case 1:
        if(z!=0)
        {
            res=(float)(x+y)/z;
            printf("The result is: %f \n",res);
        }
        else
        {
            printf("Division by zero?!\n");
        }
        break;
    case 2:
        if((z>=0)&&(y!=0))
        {
            res=(float)sqrt(z)+((float)pow(x,3)/y);
            printf("The result is: %f \n",res);
        }
        else if(z<0)
        {
            printf("Square root attempted to use negative integer\n");
        }
        else if(y=0)
        {
            printf("You cannot devide with zero\n");
        }
        else
        {
            printf("what?");
        }
        break;
    case 3:
        res=x+y+z/(float)3;
        printf("The average of x+y+z is: %f\n",res);
        break;
    case 4:
        res=x+y+z;
        for(i=1; i<res; ++i)
        {
            printf("test\n");
        }
        break;
    default:
        printf("Please input a number between 1-4!!!\n");
    }
    return 0;
}

最佳答案

问题可能出在这个语句上
scanf("%d",n);
将其更改为
scanf("%d",&n);
说明: scanf("%d",n); 不允许用户在控制台上输入,因为缺少地址运算符 &

关于C,我的 switch 语句在控制台上没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53326063/

相关文章:

c - 返回 "ENOTDIR"、 "EBUSY"等字符串的函数?

c++ - 使用 lex 和 yacc 解析文件的 C++ 程序中的编译错误

c - C中的内存分配顺序导致写入变量错误?

使用命令行参数计算矩阵行列式的 C 程序

ios - Switch 语句 VS If 语句

vba - 循环为一行中的每个新数据创建一个新工作表 - MS Excel

javascript - 如何避免在 AJAX 中的 if/else 语句中重复代码

php - 速记 switch 语句是否存在(在 PHP 中)?

javascript - 我需要一个可切换的按钮,但我已经生锈了

c# - 编译器对 switch 语句的看法是什么?