Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

what's wrong with this?

Think you can spy out that little nasty bug? Try debugging some code in here

what's wrong with this?

Postby jeezcak3 on Thu Jul 16, 2009 3:08 pm

Code: Select all
   int health = 100, damage = 4;
   for(int x = 3; x-- == 0; health - damage)
   {
      cout << health;
   }
jeezcak3
 
Posts: 8
Joined: Thu Jul 16, 2009 3:06 pm

Re: what's wrong with this?

Postby Meltra Bour on Thu Jul 16, 2009 3:35 pm

Code: Select all
   int health = 100, damage = 4;
   for(int x = 3; x-- >= 0; health = health - damage)
   {
      cout << health << "\n";
   }


That will work.

First change, "x-- >= 0" vs "x-- == 0". A for loop needs a statement that results in true, as soon as the statement is false the loop will stop, your statement does the opposite. Second change 'health = health - damage' vs 'health - damage', you where just giving the for loop a number without telling it what to do with that number ...
Meltra Bour
 
Posts: 8
Joined: Tue Jul 14, 2009 6:46 am

Re: what's wrong with this?

Postby jeezcak3 on Thu Jul 16, 2009 5:04 pm

thanks! ;]] <3
jeezcak3
 
Posts: 8
Joined: Thu Jul 16, 2009 3:06 pm

Re: what's wrong with this?

Postby antiRTFM on Thu Jul 16, 2009 9:25 pm

@Meltra Bour: BTW use the -= operator like this (health -= damage) instead of (health = health - damage)
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: what's wrong with this?

Postby Meltra Bour on Fri Jul 17, 2009 3:07 am

interesting, didn't know that trick yet
Meltra Bour
 
Posts: 8
Joined: Tue Jul 14, 2009 6:46 am


Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests