< Previous | Contents | Next >
The Taking Damage program simulates what happens to a character’s health as the character takes radiation damage. The character loses half of his health each round. Fortunately, the program only runs three rounds, so we’re spared the sad end of the character. The program inlines the tiny function that calculates the character’s 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
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";
return 0;
}
inline int radiation(int health)
{
return (health / 2);
}