1

For some reason my met variable cannot be used as a function in my last while statement, even though my other two variables can be. When i compile I get the error: '(met <= 2.0e+1)' cannot be used as a function|. How do i fix this?

// 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);{
  • Your `while` loop is completely out of whack. What you're saying is "while reg is less than or equal to 50, evaluate whether met <= 20, and call the result with (glass <= 20). At the very least, you probably want to join those conditions with either && or ||, fix your brackets, and then provide the loop with a body. – Jeremy Roman Sep 30 '12 at 23:21
  • Just as a side note, comparing floating numbers for equality (`==`) is never a good idea. It's better to check if the differ by an arbitrary small amount. – Jack Sep 30 '12 at 23:23

3 Answers3

3

You may want to balance some parentheses and add some logical operators in that while() condition. Once you do that, perhaps losing the semi-colon before the opening brace will actually break the infinite loop you're about to start executing with the appropriate values for reg, met, and glass.

while (( reg <= 50) && (met <= 20) && (glass <= 20))
{
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
3
while( reg <= 50) (met <= 20)  (glass <= 20);{

would be your problem!

That is not a valid while loop statement.

Instead, you should write:

while (( reg <= 50) && (met <= 20) && (glass <= 20)) {
//Statements for loop
}
dgund
  • 3,459
  • 4
  • 39
  • 64
2

You'll need to use && to chain together your conditions:

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

You have a similar problem in your earlier if statement:

if( met==reg==glass)

This should be:

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

But since these are floating point numbers, you should instead check that they differ only by a minimum difference.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • Thanks for the advice, would you happen to know how to how I could force a float to print exactly 4 decimal points? – Mike Shamus Sep 30 '12 at 23:36
  • 1
    @MikeShamus sure, if you just want to output 4 digits past the decimal, and you're using `cout`, then check [this answer](http://stackoverflow.com/questions/5907031/printing-the-correct-number-of-decimal-points-with-cout). – pb2q Sep 30 '12 at 23:42