CS101
CS101
2// Craps
3#include <iostream>
5using std::cout;
6using std::endl;
8#include <cstdlib>
10 #include <ctime>
11
12 using std::time;
13
15
16 int main()
17 {
18
19
enum Status { CONTINUE, WON, LOST };
24
25 switch ( sum ) {
26 case 7:
28 gameStatus = WON;
29 break;
30 case 2:
31 case 3:
33 gameStatus = LOST;
34 break;
35 default: // remember point
36 gameStatus = CONTINUE;
37 myPoint = sum;
38 cout << "Point is " << myPoint << endl;
39 break; // optional
40 }
41
42 while ( gameStatus == CONTINUE ) { // keep rolling
43 sum = rollDice();
44
45 if ( sum == myPoint ) // win by making point
46 gameStatus = WON;
47 else
48 if ( sum == 7 ) // lose by rolling 7
49 gameStatus = LOST;
50 }
51
52 if ( gameStatus == WON )
53 cout << "Player wins" << endl;
54 else
55 cout << "Player loses" << endl;
56
57 return 0;
58 }
59
60 int rollDice( void )
61 {
63
64 die1 = 1 + rand() % 6;
65 die2 = 1 + rand() % 6;
67 cout << "Player rolled " << die1 << " + " << die2
69
70 return workSum;
71 }
Player rolled 6 + 5 = 11
Player wins
Player rolled 6 + 5 = 11
Player wins
Player rolled 4 + 6 = 10
Point is 10
Player rolled 2 + 4 = 6
Player rolled 6 + 5 = 11
Player rolled 3 + 3 = 6
Player rolled 6 + 4 = 10
Player wins
Player rolled 1 + 3 = 4
Point is 4
Player rolled 1 + 4 = 5
Player rolled 5 + 4 = 9
Player rolled 4 + 6 = 10
Player rolled 6 + 3 = 9
Player rolled 1 + 2 = 3
Player rolled 5 + 2 = 7
Player loses
Storage Classes
• Storage class specifiers
– Storage class
• Where object exists in memory
– Scope
• Where object is referenced in program
– Linkage
• Where an identifier is known
• Automatic storage
– Object created and destroyed within its block
– auto
• Default for local variables.
• Example:
auto float x, y;
– register
• Tries to put variables into high-speed registers
– Can only be used with local variables and parameters
Storage Classes
• Static storage
– Variables exist for entire program execution
– static
• Local variables defined in functions
• Keep value after function ends
• Only known in their own function
– Extern
• Default for global variables and functions.
• Known in any function
Identifier Scope Rules
• File scope
– Defined outside a function, known in all functions
– Examples include, global variables, function definitions and
functions prototypes
• Function scope
– Can only be referenced inside a function body
– Only labels (start:, case:, etc.)
• Block scope
– Declared inside a block. Begins at declaration, ends at }
– Variables, function parameters (local variables of function)
– Outer blocks “hidden” from inner blocks if same variable name
• Function prototype scope
– Identifiers in parameter list
– Names in function prototype optional, and can be used anywhere
1// Fig. 3.12: fig03_12.cpp
3#include <iostream>
5using std::cout;
6using std::endl;
11
15 {
17
18 cout << "local x in outer scope of main is " << x << endl;
19
21 int x = 7;
22
23 cout << "local x in inner scope of main is " << x << endl;
25
26 cout << "local x in outer scope of main is " << x << endl;
27
34
35 cout << "local x in main is " << x << endl;
36
37 return 0;
38 } Local automatic
39
variables are created
40 void a( void )
41 {
and destroyed each time
42 int x = 25; // initialized each time a is called a is called.
43
45 << " after entering a" << endl; local x in a is 25 after entering a
46 ++x; local x in a is 26 before exiting a
47 cout << "local x in a is " << x
49 }
50
51 void b( void )
52 {
57 ++x;
ends.
local static x is 50 on entering b
58 cout << "local static x is " << x local static x is 51 on exiting b
59 << " on exiting b" << endl;
60 }
68 }
local x in outer scope of main is 5
local x in inner scope of main is 7
local x in outer scope of main is 5
local x in a is 25 after entering a
local x in a is 26 before exiting a
local static x is 50 on entering b
local static x is 51 on exiting b
global x is 1 on entering c
global x is 10 on exiting c
local x in a is 25 after entering a
local x in a is 26 before exiting a
local static x is 51 on entering b
local static x is 52 on exiting b
global x is 10 on entering c
global x is 100 on exiting c
local x in main is 5
References
Dietal and Dietal : How to Program C++
3rd Edition