home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / source / chapter04 / heros_inventory2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-04-11  |  1.2 KB  |  47 lines

  1. // Hero's Inventory 2.0
  2. // Demonstrates vectors
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     vector<string> inventory;
  13.     inventory.push_back("sword");
  14.     inventory.push_back("armor");
  15.     inventory.push_back("shield");
  16.  
  17.     cout << "You have " << inventory.size() << " items.\n";
  18.  
  19.     cout << "\nYour items:\n";
  20.     for (int i = 0; i < inventory.size(); ++i)
  21.         cout << inventory[i] << endl;
  22.  
  23.     cout << "\nYou trade your sword for a battle axe.";
  24.     inventory[0] = "battle axe";
  25.     cout << "\nYour items:\n";
  26.     for (int i = 0; i < inventory.size(); ++i)
  27.         cout << inventory[i] << endl;
  28.  
  29.     cout << "\nThe item name '" << inventory[0] << "' has ";
  30.     cout << inventory[0].size() << " letters in it.\n";
  31.  
  32.     cout << "\nYour shield is destroyed in a fierce battle.";
  33.     inventory.pop_back();
  34.     cout << "\nYour items:\n";
  35.     for (int i = 0; i < inventory.size(); ++i)
  36.         cout << inventory[i] << endl;
  37.  
  38.     cout << "\nYou were robbed of all of your possessions by a thief.";
  39.     inventory.clear();
  40.     if (inventory.empty())
  41.         cout << "\nYou have nothing.\n";
  42.     else
  43.         cout << "\nYou have at least one item.\n";
  44.  
  45.  return 0;
  46. }
  47.