"错误:在'< ='之前预期的主要表达令牌"我究竟做错了什么?

时间:2017-09-20 23:43:32

标签: c++ compiler-errors syntax-error

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

#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|

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

此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..."
                                   ^^^

答案 1 :(得分:0)

您希望明确指出bmin <= 25,而不是在没有主题的情况下说<=25

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