tags:

views:

302

answers:

4

NEVER MIND- IT WAS MY OWN DUMB MISTAKE, CONFUSED THE VARIABLES

I have the following while loop:

while (((pi.getFunds() - rmiPrice) > 0) && (rmi.getInventory() > 0))
{
}

pi.getFunds() is a double that represents how much money a fictional object (pi) has, rmiPrice is a double that represents a price of a product (rmi) that pi wants to buy. rmi.getInventory checks how much rmi is in the inventory- what I'm trying to do is this- while pi can afford to buy 1 more rmi AND there is at least 1 rmi in inventory, execute what is in the while loop.

What seems to be happening instead is that the && is not being recognized as either the inventory or the funds are allowed to go below 0 as long as the other is positive- any idea what I'm doing wrong?

I've also tried this:

int rmiInv = rmi.getInventory();
double piFunds = pi.getFunds();

while (((piFunds - rmiPrice) > 0) && (rmiInv > 0)) {}

And tried playing around with the bracket combos

while ((piFunds - rmiPrice) > 0 && rmiInv > 0) {}

while (piFunds - rmiPrice > 0 && rmiInv > 0) {}

nothing seems to work,

Thank you!

+2  A: 

precision issues while subtracting an integer from a double..

Vijay Dev
That would also be my guess
dmeister
But there is no subtraction of integer from double. The subtraction is between doubles.
Chris Jester-Young
yes, i mistook rmiPrice for an integer. However, I guess precision is still an issue here.
Vijay Dev
A: 

Can you, before the loop and at each iteration, print out the values of piFunds, rmiPrice, and rmiInv, and paste it here? I suspect numeric overflow.

Chris Jester-Young
A: 

Oh wow, I think I just used the wrong variables.... sorry guys.... How do I delete the question?

Olegious
Under the tags in the question, there should be options like edit, delete, etc. Pick delete. :-)
Chris Jester-Young
Heh, too many answers to delete....
Olegious
A: 

You are not doing anything wrong. The while: while(expression) { // stuff }, where expression must evaluate to boolean. In your case the expression: ((pi.getFunds() - rmiPrice) > 0) && (rmi.getInventory() > 0) must evaluate to boolean true to have the code inside the loop executed. If the expression is false, it is never executed. Again, in your case, both operands MUST evaluate to true.