c++ - “error: expected primary-expression before ' <= ' token”我在做什么错?

标签 c++ compiler-errors syntax-error

这是我的代码。老实说,我大约十天前开始学习C++,并且刚刚开始学习if语句。抱歉,如果我的语法很糟糕。

#include<iostream>


float bmi(float, float);

int main(){

    float weight, height;

    std::cout << "Input your weight(In pounds)" << std::endl;
    std::cin >> weight;
    std::cout << "Input your height(In inches)" << std::endl;
    std::cin >> height;

    bmi(weight, height);

    return 0;
}

float bmi(float n1,float n2){
    float bmin;
    bmin = (n1*703)/(n2*n2);
    std::cout << "Your BMI is: " << bmin << std::endl;

    if(bmin <= 18.49){
        std::cout << "You are underweight!" << std::endl;
    }
    else if(bmin >=18.5 and <= 25){
        std::cout << "You have normal weight!" << std::endl;
    }
    else if(bmin >=25.01 and <=29.99){
        std::cout << "You are overweight." << std::endl;
    }
    else if (bmin >=30){
        std::cout << "You are obese..."
    }
}

对于我的一生,我无法弄清楚这里出了什么问题。
哦,这是错误。
错误在第23和26行上。
C:\Users\Finnegan\Desktop\Computer Science 3-4\Computer Science\fm2-
2.cpp|23|error: expected primary-expression before '<=' token|

然后我在第31行有一个错误
C:\Users\Finnegan\Desktop\Computer Science 3-4\Computer Science\fm2-

2.cpp|31|error: expected ';' before '}' token|

在此先感谢您的帮助!

最佳答案

此else语句(和其他类似语句)中的条件

else if(bmin >=18.5 and <= 25){

相当于
else if( ( bmin >=18.5 ) and ( <= 25 )){

因此,编译器会发出错误,因为它期望使用有效的表达式而不是<= 25构造。

显然你的意思是
else if(bmin >=18.5 and bmin <= 25){

考虑到函数bmi的返回类型为float,但不返回任何内容。
float *bmi*(float n1,float n2);

在此声明中,您忘记了放置分号。
else if (bmin >=30){
    std::cout << "You are obese..."
                                   ^^^

关于c++ - “error: expected primary-expression before ' <= ' token”我在做什么错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46333285/

相关文章:

c++ - 静态类成员显示令人困惑的语法错误

c++ - 指针和方法的奇怪用法

c++ - 是否有必要锁定一个*仅从一个线程写入*而*仅从*另一个线程读取的数组?

windows - 在Windows上安装dlib期间发生Cmake错误?

visual-c++ - 错误 C2143 VC++12

node.js - 带有nodejs的Requirejs不告诉我错误的行号,只是错误

php - MYSQL更新多表报错

ios - 快速函数调用中是否有必需的参数顺序

c++ - 编译错误声明我的 char vector 数组

c++ - 有没有办法找到像sizeof facelity这样的动态内存大小?