Advertisement
TermSpar

Vectors

Apr 19th, 2016
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. //VECTOR++ CODE WRITTEN BY BEN BOLLINGER
  6.  
  7. using namespace std;
  8.  
  9. void createVec(vector<int>&);       //Creates and sets values for a new vector of integers
  10. void showVec(const vector<int>&);   //Displays the vector
  11. void mulVec(const vector<int>&);    //Multiplies vector values with user input
  12.  
  13. void createString(vector<string>&);         //Creates and sets values for a new vector of string
  14. void showString(const vector<string>&);     //Displays the vector
  15. void joinString(const vector<string>&);     //Joins all string pieces together
  16. void insertString(vector<string>&);         //Inserts new string into vector
  17.  
  18. void insertInt(vector<int>&);       //Inserts new integer into vector
  19. void replaceInt(vector<int>&);      //Replaces existing integer with new integer in vector
  20.  
  21. int main()
  22. {
  23.     cout << "----- WELCOME TO VECTOR++ BY BEN BOLLINGER v1.3 -----" << endl;
  24.     cout << endl;
  25.     cout << "TYPE string OR int" << endl;
  26.    
  27.     string vecType;
  28.     cin >> vecType;
  29.    
  30.     if(vecType == "string"){
  31.        
  32.         cout << "ENTER STRING (x to stop): ";
  33.         vector<string> userString;
  34.        
  35.         createString(userString);
  36.         showString(userString);
  37.        
  38.         cout << "------------------------" << endl;
  39.         cout << "JOIN STRING? (yes or no): ";
  40.        
  41.         string yOr;
  42.         cin >> yOr;
  43.        
  44.         if(yOr == "yes"){
  45.             cout << "------------------------" << endl;
  46.             joinString(userString);
  47.         }else{
  48.             cout << "------------------------" << endl;
  49.             cout << "INSERT CHAR? (yes or no): ";
  50.            
  51.             string inSer;
  52.             cin >> inSer;
  53.            
  54.             if(inSer == "yes"){
  55.                 cout << "------------------------" << endl;
  56.                 insertString(userString);
  57.             }else{
  58.                 cout << "------------------------" << endl;
  59.                 cout << "COMPLETED";
  60.             }
  61.         }
  62.        
  63.     }else if(vecType == "int"){
  64.        
  65.         cout << "ENTER INTEGERS (-1 to stop): ";
  66.         vector<int> userNums;
  67.        
  68.         createVec(userNums);
  69.         showVec(userNums);
  70.        
  71.         cout << "------------------------" << endl;
  72.         cout << "MULTIPLY? (enter yes or no): ";
  73.        
  74.         string yOr;
  75.         cin >> yOr;
  76.        
  77.         if(yOr == "yes"){
  78.  
  79.             cout << "------------------------" << endl;
  80.             mulVec(userNums);    
  81.  
  82.         }else{
  83.  
  84.             cout << "------------------------" << endl;
  85.             cout << "REPLACE NUMBERS? (yes or no): ";
  86.             string yOr;
  87.             cin >> yOr;
  88.  
  89.             if(yOr == "yes"){
  90.  
  91.                 replaceInt(userNums);
  92.  
  93.             }else{
  94.  
  95.                 cout << "------------------------" << endl;
  96.                 cout << "COMPLETE" << endl;
  97.  
  98.             }
  99.         }
  100.        
  101.     }else{
  102.        
  103.         cout << "INVALID TYPE" << endl;    
  104.        
  105.     }
  106.    
  107.  
  108.     system("pause");
  109. }
  110.  
  111. void createVec(vector<int>& newVec){
  112.  
  113.     int uInput;
  114.     cin >> uInput;
  115.    
  116.      while(uInput != -1){
  117.         newVec.push_back(uInput);
  118.         cin >> uInput;
  119.     }
  120. }
  121.  
  122. void showVec(const vector<int>& newVec){
  123.     cout << "VECTOR BEGINNING" << endl;
  124.     cout << "------------------------" << endl;
  125.    
  126.     cout << "CONTENTS: ";
  127.    
  128.     for(unsigned int i = 0; i < newVec.size(); i++){
  129.         cout << newVec[i] << " ";    
  130.     }  
  131.    
  132.     cout << endl;
  133.     cout << "------------------------" << endl;
  134.     cout << "VECTOR DISPLAY COMPELETE" << endl;
  135. }
  136.  
  137. void createString(vector<string>& newVec){
  138.    
  139.     string uInput;
  140.     cin >> uInput;
  141.    
  142.      while(uInput != "x"){
  143.         newVec.push_back(uInput);
  144.         cin >> uInput;
  145.     }
  146. }
  147.  
  148. void showString(const vector<string>& newVec){
  149.     cout << "VECTOR BEGINNING:" << endl;
  150.     cout << "------------------------" << endl;
  151.    
  152.     cout << "CONTENTS: ";
  153.      
  154.     for(unsigned int i = 0; i < newVec.size(); i++){
  155.         cout << newVec[i] << " ";    
  156.     }  
  157.    
  158.     cout << endl;
  159.     cout << "------------------------" << endl;
  160.     cout << "VECTOR DISPLAY COMPELETE" << endl;
  161. }
  162.  
  163. void mulVec(const vector<int>& newVec){
  164.    
  165.     cout << "BY WHAT NUMBER?: ";
  166.    
  167.     int mulNum;
  168.     cin >> mulNum;
  169.     cout << "------------------------" << endl;
  170.    
  171.     cout << "NEW CONTENTS: ";
  172.    
  173.     for(unsigned int i = 0; i < newVec.size(); i++){
  174.         cout << newVec[i] * mulNum << " ";      
  175.     }
  176.    
  177.     cout << endl;
  178.     cout << "------------------------" << endl;
  179.     cout << "VECTOR DISPLAY COMPELETE" << endl;
  180.        
  181. }
  182.  
  183. void joinString(const vector<string>& newVec){
  184.    
  185.     string joinedS;
  186.    
  187.     for(unsigned int i = 0; i < newVec.size(); i++){
  188.         joinedS += newVec[i];
  189.     }
  190.    
  191.     cout << "JOINED STRING: " << joinedS << endl;
  192. }
  193.  
  194. void insertString(vector<string>& newVec){
  195.    
  196.     cout << "ENTER VECTOR POSITION: ";
  197.    
  198.     int vecPos;
  199.     cin >> vecPos;
  200.    
  201.     cout << "------------------------" << endl;
  202.    
  203.     cout << "ENTER DATA TO INSERT: ";
  204.    
  205.     string intData;
  206.     cin >> intData;
  207.    
  208.     if(intData.length() == 1){
  209.         newVec.insert(newVec.begin() + vecPos, intData);
  210.    
  211.     cout << "------------------------" << endl;
  212.    
  213.     cout << "NEW CONTENTS: ";
  214.    
  215.     for(unsigned int i = 0; i < newVec.size(); i++){
  216.  
  217.         cout << newVec[i];
  218.  
  219.     }
  220.  
  221.     }else{
  222.         cout << "------------------------" << endl;
  223.         cout << "INSERT MAY ONLY BE 1 CHAR";
  224.     }
  225. }
  226.  
  227. void replaceInt(vector<int>& newVec){
  228.  
  229.     cout << "------------------------" << endl;
  230.     cout << "ENTER NUMBER TO REPLACE: ";
  231.     int repNum;
  232.     cin >> repNum;
  233.     cout << "------------------------" << endl;
  234.  
  235.     cout << "ENTER NUMBER TO REPLACE WITH: ";
  236.     int newNum;
  237.     cin >> newNum;
  238.     cout << "------------------------" << endl;
  239.  
  240.     for(unsigned int i = 0; i < newVec.size(); i++){
  241.        
  242.         if(newVec[i] == repNum){
  243.             newVec[i] = newNum;
  244.         }
  245.         cout << newVec[i] << " ";
  246.  
  247.     }
  248.     cout << endl;
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement