1

出于某种原因,我的met 变量不能用作我最后一个while 语句中的函数,即使我的其他两个变量可以。编译时出现错误:'(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);{
4

3 回答 3

3

您可能想要平衡一些括号并在该while()条件下添加一些逻辑运算符。一旦你这样做了,也许在左大括号之前丢失分号实际上会破坏你即将开始执行的无限循环,并使用适当的值reg,metglass.

while (( reg <= 50) && (met <= 20) && (glass <= 20))
{
}
于 2012-09-30T23:22:35.133 回答
3
while( reg <= 50) (met <= 20)  (glass <= 20);{

会是你的问题!

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

相反,你应该写:

while (( reg <= 50) && (met <= 20) && (glass <= 20)) {
//Statements for loop
}
于 2012-09-30T23:25:25.750 回答
2

您需要使用&&将您的条件链接在一起:

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

您之前的if陈述中有类似的问题:

if( met==reg==glass)

这应该是:

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

但由于这些是浮点数,您应该检查它们是否仅相差最小差异。

于 2012-09-30T23:22:53.147 回答