2017-09-20 66 views
-2

所以这里是我的代码。老实说,我大约在十天前开始学习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| 

预先感谢您的帮助!

+1

'BMIN> = 18.5和<= 25'无法做到这一点。改用'bmin> = 18.5和bmin <= 25'。 – user4581301

+3

我希望你没有计划在每次你的C++程序不编译时在stackoverflow.com上发布一个问题,你不知道为什么。这需要你很多很多年的时间来学习C++。 –

+0

函数'bmi'的返回值在哪里? – Raindrop7

回答

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..." 
            ^^^ 
+0

@ Raindrop7编译器根据C++标准提供了替代权标。请参阅C++标准中的“2.5替代权标”。 –

+0

@Finnegan根本没有。不客气:) –

+0

不同意你的第一个“等价于” - 唯一的等价是它们都是语法错误 –

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..." 
    } 
相关问题