< Previous | Contents | Next >
Next I get some information from the player.
int main()
{
const int GOLD_PIECES = 900;
int adventurers, killed, survivors; string leader;
34 Chapter 1 n Types, Variables, and Standard I/O: Lost Fortune
//get the information
cout << "Welcome to Lost Fortune\n\n";
cout << "Please enter the following for your personalized adventure\n";
cout << "Enter a number: "; cin >> adventurers;
cout << "Enter a number, smaller than the first: "; cin >> killed;
survivors = adventurers - killed;
cout << "Enter your last name: "; cin >> leader;
GOLD_PIECES is a constant that stores the number of gold pieces in the fortune the adventurers seek. adventurers stores the number of adventurers on the quest. killed stores the number that are killed on the journey. I calculate survivors for the number of adventurers that remain. Finally, I get the player’s last name, which I’ll be able to access through leader.
Tra p
This simple use of cin to get a string from the user only works with strings that have no whitespace in them (such as tabs or spaces). There are ways to compensate for this, but that really requires a discussion of something called streams, which is beyond the scope of this chapter. So, use cin in this way, but be aware of its limitations.