< Previous | Contents | Next >
Introducing the Game Stats 2.0
The Game Stats 2.0 program manipulates variables that represent game stats and displays the results. Figure 1.6 shows the program in action.
You can download the code for this program from the Course Technology website (www.courseptr.com/downloads). The program is in the Chapter 1 folder; the filename is game_stats2.cpp.
// Game Stats 2.0
// Demonstrates arithmetic operations with variables
#include <iostream> using namespace std;
Performing Arithmetic Operations with Variables 25
Figure 1.6
Each variable is altered in a different way.
int main()
{
unsigned int score = 5000;
cout << "score: " << score << endl;
//altering the value of a variable score = score + 100;
cout << "score: " << score << endl;
//combined assignment operator score += 100;
cout << "score: " << score << endl;
//increment operators int lives = 3; þþlives;
cout << "lives: " << lives << endl;
lives = 3; livesþþ;
cout << "lives: " << lives << endl;
26 Chapter 1 n Types, Variables, and Standard I/O: Lost Fortune
lives = 3;
int bonus = þþlives * 10;
cout << "lives, bonus = " << lives << ", " << bonus << endl;
lives = 3;
bonus = livesþþ * 10;
cout << "lives, bonus = " << lives << ", " << bonus << endl;
//integer wrap around score = 4294967295;
cout << "\nscore: " << score << endl;
þþscore;
cout << "score: " << score << endl;
return 0;
}
Tra p
When you compile this program, you may get a warning similar to, “[Warning] this decimal constant is unsigned.” Fortunately, the warning does not stop the program from compiling and being run. The warning is the result of something called integer wrap around that you’ll probably want to avoid in your own programs; however, the wrap around is intentional in this program to show the results of the event. You’ll learn about integer wrap around in the discussion of this program, in the section “Dealing with Integer Wrap Around.”