不能用作功能?

时间:2012-09-30 23:20:03

标签: c++ macos

由于某种原因,我的met变量不能在我的last语句中用作函数,即使我的其他两个变量也可以。当我编译时,我得到错误:'(met< = 2.0e + 1)'不能用作函数|。我该如何解决这个问题?

// Garbage Collection. Michael Heusner.
#include <iostream>
#include <cmath>

using namespace std;

int main(){
int reg_lim, met_lim, glass_lim;
double reg, glass, met;
double total;
double reg_ratio, glass_ratio, met_ratio;

reg_lim= 50;
glass_lim= 20;
met_lim= 20;


cout << "How much regular, glass, and metal garbage do you have?" << endl;
cin>> reg;
cin>> glass;
cin>> met;

total= met+glass+reg;

cout<< "The total number of bags is "<< total<< endl;

met_ratio= met/total;
reg_ratio= reg/total;
glass_ratio= glass/total;

cout<< "The metal ratio is "<< met_ratio<< endl;
cout<< "The glass ratio is "<< glass_ratio<< endl;
cout<< "The regular ratio is "<< reg_ratio<< endl;
if( met==reg==glass)
{
cout<< "All garbage amounts are the same."<< endl;
}
else if (reg> glass && met)
{
cout<< "Regular is the largest."<< endl;
}
else if (glass> met && reg)
{
cout<< "Glass is the largest."<< endl;
}
else if (met> glass && reg)
{
cout<< "Metal is the largest."<< endl;
}


while( reg <= 50) (met <= 20)  (glass <= 20);{

3 个答案:

答案 0 :(得分:3)

您可能需要平衡一些括号并在while()条件下添加一些逻辑运算符。一旦你这样做,也许在开始大括号之前丢失分号实际上会打破你将要开始执行regmet和{{{{}}的适当值的无限循环。 1}}。

glass

答案 1 :(得分:3)

while( reg <= 50) (met <= 20)  (glass <= 20);{

将是你的问题!

这不是一个有效的while循环语句。

相反,您应该写:

while (( reg <= 50) && (met <= 20) && (glass <= 20)) {
//Statements for loop
}

答案 2 :(得分:2)

您需要使用&&将条件链接起来:

while ((reg <= 50) && (met <= 20) && (glass <= 20))

您之前的if声明中存在类似的问题:

if( met==reg==glass)

这应该是:

if ((met==reg) && (reg==glass))

但由于这些是浮点数,因此您应该检查它们是否只有最小差异。