Advertisement
TermSpar

Vector Iterators and Dynamic Memory Allocation

May 18th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 KB | None | 0 0
  1. //C++ Vector Iterations and Dynamic Memory: By Ben Bollinger
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. char askYesNo(string myQuestion);
  12. char attackDefend(string question);
  13. string changeWeapon(vector<string> weapon);
  14.  
  15. int main()
  16. {
  17.     //Create weapon choice:
  18.     vector<string> inv;
  19.     inv.push_back("sword");
  20.     inv.push_back("knife");
  21.     inv.push_back("gun");
  22.    
  23.     //Declare vector iteratiors:
  24.     vector<string>::iterator myIter;
  25.     vector<string>::const_iterator iter;
  26.    
  27.     cout << "Your Items:\n";
  28.     cout << "-----------\n";
  29.    
  30.     //List out items:
  31.     for(iter = inv.begin(); iter != inv.end(); iter++)
  32.     {
  33.         cout << *iter << endl;    
  34.     }
  35.    
  36.     cout << "-----------\n";
  37.    
  38.     //Let user select weapon:
  39.     string userWeapon;
  40.     string change = changeWeapon(inv);
  41.     {
  42.         if(change == "sword")
  43.         {
  44.             cout << "You chose sword\n";
  45.             userWeapon = "sword";
  46.            
  47.         }else if(change == "knife")
  48.         {
  49.             cout << "You chose knife\n";
  50.             userWeapon = "knife";
  51.            
  52.         }else if(change == "gun")
  53.         {
  54.             cout << "You chose gun\n";
  55.             userWeapon = "gun";
  56.         }else
  57.         {
  58.             //Set random weapon if user types invalid input:
  59.             srand(static_cast<unsigned int>(time(0))); //Seed random number with time.
  60.             int rndWeapon = rand() % 3 + 1; //Generate number.
  61.             int realNum = rndWeapon;
  62.            
  63.             //Decision structure:
  64.             if(realNum == 1)
  65.             {
  66.                 userWeapon = "sword";
  67.                
  68.             }else if(realNum == 2)
  69.             {
  70.                 userWeapon = "knife";
  71.                
  72.             }else if(realNum == 3)
  73.             {
  74.                 userWeapon = "gun";
  75.             }
  76.            
  77.             cout << "Invaid Option, your weapon is now: " << userWeapon << "\n";
  78.         }
  79.     }
  80.    
  81.     //Setup first enemy attack
  82.     char enemy1 = attackDefend("Enemy attacks, do you 'attack' or 'defend'?");
  83.     {
  84.         if(enemy1 == 'a'){
  85.            
  86.             if(userWeapon == "sword")
  87.             {
  88.                 cout << "You perished\n"; //If sword, then death.
  89.                
  90.             }else if(userWeapon == "knife")
  91.             {
  92.                 cout << "You never stood a chance\n"; //If knife, then death.
  93.                
  94.             }else if(userWeapon == "gun")
  95.             {
  96.                 cout << "You killed the enemy\n"; //If gun, then success.
  97.             }
  98.          
  99.         }else if(enemy1 == 'd'){
  100.             cout << "Your defenses were breached\n"; //If defend, then death.
  101.         }  
  102.     }
  103.    
  104.     //Ask user to create a new weapon:
  105.     char createWeapon = askYesNo("Create a weapon?");
  106.     {
  107.         if (createWeapon == 'y')
  108.         {
  109.             int replaceWeap;
  110.             cout << "What weapon do you want to replace? (1: sword, 2: knife, 3: gun): ";
  111.             cin >> replaceWeap; //Set weapon to replace.
  112.            
  113.             string newWeap;
  114.             cout << "New weapon name: ";
  115.             cin >> newWeap; //Set name of new weapon.
  116.            
  117.             //Weapon replace logic:
  118.             myIter = inv.begin() + replaceWeap - 1;
  119.             string oldWeap = *myIter;
  120.             *myIter = newWeap;
  121.            
  122.             cout << "\n++++++++++++++++++++\n";
  123.             cout << "Successfuly replaced '" << oldWeap << "' with '" << newWeap << "'\n";
  124.             cout << "++++++++++++++++++++\n";
  125.            
  126.             //Display new item set:
  127.             cout << "\nNew Items: \n";
  128.             cout << "------------\n";
  129.             for(iter = inv.begin(); iter != inv.end(); iter++)
  130.             {
  131.                 cout << *iter << endl;    
  132.             }
  133.             cout << endl;
  134.            
  135.         }else
  136.         {
  137.             cout << "Game Over" << endl;
  138.         }
  139.     }
  140. }
  141.  
  142. //Enemy attack function:
  143. char attackDefend(string question)
  144. {
  145.     char response;
  146.     cout << question << " (a/d): ";  
  147.     cin >> response;
  148.    
  149.     return response;
  150. }
  151.  
  152.  
  153. //Change user weapon function:
  154. string changeWeapon(vector<string> inventory)
  155. {
  156.     string weapon;
  157.     string choice;
  158.     cout << "Enter what weapon you would like to use: ";
  159.     cin >> choice;
  160.    
  161.     for(int i = 0; i < inventory.size(); i++)
  162.     {
  163.         if(choice == inventory[i])
  164.         {
  165.             weapon = choice;
  166.         }
  167.     }
  168.     return weapon;
  169. }
  170.  
  171. //Ask a question function:
  172. char askYesNo(string myQuestion)
  173. {
  174.     char answer;
  175.     cout << myQuestion << " (y/n): ";
  176.     cin >> answer;
  177.    
  178.     return answer;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement