c - 我无法在 if 语句中使用字符名称

标签 c codeblocks

#include<stdio.h>
#include<conio.h>
void main()
{
    char sexo;
    float alt,peso;
    printf("Qual o seu sexo? (M/F)");
    sexo=getch();
    printf("Insira a sua altura: ");
    scanf("&f", &alt);
    if (sexo=m||sexo=M||sexo=f||sexo=F)
    {
        if (sexo=m||sexo=M)
        peso=(72.7*alt)-58;
        else
        peso=(62.1*alt)-44.7;
        printf("O peso recomendado para o seu sexo e %.2fKg",peso);
    }
    else
        printf("Sexo invalido");
}

一旦我尝试编译它,我就会在第 11 行收到错误消息:

error: 'm' undeclared (first use in this function)
error: 'M' undeclared (first use in this function)
error: 'f' undeclared (first use in this function)
error: 'F' undeclared (first use in this function)

||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===||

有人知道发生了什么吗?

最佳答案

你使用 m & M & f & F 的方式就像它们是变量而不是字符 这就是你想要做的

2- 比较运算符是 == 而不是 =(这是赋值运算符)

if (sexo=='m'||sexo=='M'||sexo=='f'||sexo=='F')

关于c - 我无法在 if 语句中使用字符名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54916063/

相关文章:

C 进程 fork()

opencv - 代码块 OpenCV 构建失败

c - 使用 getchar() 时多次迭代健全性检查,我该如何避免?

c++ - Void 类型函数不打印出结构成员变量

git - Code::Blocks git 插件

c++ - 如何在 gstreamer 插件中使用 c++ 模块?

c - 需要帮助了解\n、\b 和\r 将如何呈现 printf 输出

c - 为什么我在这个 STACK 程序中得到这个数字而不是值?

为任何类型的数组创建 min() 宏

c++ - 使用(mock)头文件作为模板时的“Multiple definition”