< Previous | Contents | Next >

Introducing the Taking Damage

Program

The Taking Damage program simulates what happens to a characters health as the character takes radiation damage. The character loses half of his health each round. Fortunately, the program only runs three rounds, so were spared the sad end of the character. The program inlines the tiny function that calculates the characters new health. Figure 5.7 shows the program results.

You can download the code for this program from the Course Technology website (www.courseptr.com/downloads). The program is in the Chapter 5 folder; the filename is taking_damage.cpp.

178 Chapter 5 n Functions: Mad Lib


image

Figure 5.7

The character approaches his demise quite efficiently as his health decreases through an inlined function.


// Taking Damage

// Demonstrates function inlining #include <iostream>

int radiation(int health); using namespace std;

int main()

{

int health = 80;

cout << "Your health is " << health << "\n\n";


health = radiation(health);

cout << "After radiation exposure your health is " << health << "\n\n";


health = radiation(health);

cout << "After radiation exposure your health is " << health << "\n\n";


health = radiation(health);

cout << "After radiation exposure your health is " << health << "\n\n";

Inlining Functions 179


return 0;

}


inline int radiation(int health)

{

return (health / 2);

}