HomeForumsWhat's newResources 
 
 
C++
RitualOfTheTrout - Dec 5, 2003
 RitualOfTheTrout Dec 5, 2003
Whats wrong with this code? for some reason it is evaluating each else statement..

Just for useless info this is part of my Vending machine project in Rose Real Time..

Code:
  
if (ItemNumber == 1 && Pepsi > 0)

{

  Pepsi--;

  log.log ("Thank You For Purchasing a Pepsi!");

  transaction.newComplete().send();

}

else  

{

  log.log ("Sorry, Item is currently out of stock.");

  cancle.newCancle(ItemNumber).send();

}

	

if (ItemNumber == 2 && Fizzle > 0)

{

  Fizzle--;

  log.log ("Thank You For Purchasing a Fizzle!");

  transaction.newComplete().send();

}

else 

{

  log.log ("Sorry, Item is currently out of stock.");

  cancle.newCancle(ItemNumber).send();

}

if (ItemNumber == 3 && Squishy > 0)

{

  Squishy--;

  log.log ("Thank You For Purchasing a Squishy!");

  transaction.newComplete().send();

}

else 

{

  log.log ("Sorry, Item is currently out of stock.");

  cancle.newCancle(ItemNumber).send();

}

if (ItemNumber == 4 && Yohoo > 0)

{

  Yohoo--;

  log.log ("Thank You For Purchasing a Yohoo!");

  transaction.newComplete().send();

}

else 

{

  log.log ("Sorry, Item is currently out of stock.");

  cancle.newCancle(ItemNumber).send();

}

 RitualOfTheTrout Dec 5, 2003
What i want to happen is that it evaluates the first if.. then if its false it moves on tot he second if.. and so on.. but i dont know why runs all the else statements?

 mtxblau Dec 5, 2003
Shouldn't you use a case1, case2, case3 set-up?

The reason why it's evaluating every else is because since both values aren't met, it'll automatically go to the else, for all of them. What you may want to do is use a do - while loop for each (or so I think - what do I know?).

EDIT: Looking at it, you may just want to do a nested if loop.

Code:
  
if (ItemNumber == 1)

{

  if (Pepsi > 0){ ///}

  if (Pepsi == 0) {///}

}

It's not pretty, but you get the job done.

Since the two variables you're passing are rarely going to match (probably not ever, since it won't evaluate the if) it'll always evaluate the else.

 RitualOfTheTrout Dec 5, 2003
I fixed it... it's probably not the most effecient way in the least, but it does what I want. The main goal is to test our use of Rose, not C++ knowledge... so in this case, a little bad programming shouldn't hurt me hehe...

Code:
  
if ((ItemNumber == 1) &&( Pepsi > 0))

{

  Pepsi--;

  log.log ("Thank You For Purchasing a Pepsi!");

  transaction.newComplete().send();

}

else if ((Pepsi == 0) && (ItemNumber == 1)) 	

{

  log.log ("Sorry, Item is currently out of stock11.");

}

	

if ((ItemNumber == 2) && (Fizzle > 0))

{

  Fizzle--;

  log.log ("Thank You For Purchasing a Fizzle!");

  transaction.newComplete().send();

}

else if ((Fizzle == 0) && (ItemNumber == 2))

{

  log.log ("Sorry, Item is currently out of stock.");

}

if ((ItemNumber == 3) && (Squishy > 0))

{

  Squishy--;

  log.log ("Thank You For Purchasing a Squishy!");

  transaction.newComplete().send();

}

else if ((Squishy == 0) && ( ItemNumber == 3))

{

  log.log ("Sorry, Item is currently out of stock.");

}

	

if ((ItemNumber == 4) && (Yohoo > 0))

{

  Yohoo--;

  log.log ("Thank You For Purchasing a Yohoo!");

  transaction.newComplete().send();

}

else if ((Yohoo == 0) && (ItemNumber == 4))

{

  log.log ("Sorry, Item is currently out of stock.");

}

 Mask of Destiny Dec 6, 2003
You can take out one of the conditions in your else ifs.

 antime Dec 6, 2003
Only the amount check, or you'll get extra log messages if an item has run out before. MTXBlau's suggestion is cleaner. Personally I'd put the items in an array or list so you'd get by with only one check.