Advertisement
TermSpar

Random Pairs Generator

Nov 11th, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <fstream>
  8.  
  9. int main()
  10. {
  11.  
  12.     //Define variables:
  13.     std::vector<std::string> classList;
  14.     std::vector<std::string> newList;
  15.     std::string file = "";
  16.     std::ifstream myFile(file);
  17.     std::string line = "";
  18.     int count = 0;
  19.  
  20.     //Main loop:
  21.     while (file != "exit") {
  22.         std::cout << "Enter file directory (type 'exit' to end process): ";
  23.         std::cin >> file;
  24.  
  25.         //Add names from file to classList:
  26.         if (myFile.is_open()) {
  27.             while (std::getline(myFile, line)) {
  28.                 classList.push_back(line);
  29.                 count++;
  30.             }
  31.         }
  32.  
  33.         //Ensure the list has an even number of names:
  34.         if (count % 2 == 0 && file != "exit") {
  35.             std::srand(unsigned(std::time(0)));
  36.  
  37.             //Randomize the classList:
  38.             std::random_shuffle(classList.begin(), classList.end());
  39.  
  40.             //Pair the names that are next to each other and add it to the newList:
  41.             for (int i = 0; i < classList.size() - 1; i++) {
  42.                 std::string str1 = classList[i] + "\n" + classList[i + 1];
  43.                 i++;
  44.                 newList.push_back(str1);
  45.             }
  46.  
  47.             //Print out pairs:
  48.             std::cout << "\nRandomly generated pairs:\n\n";
  49.             for (int i = 0; i < newList.size(); i++) {
  50.                 std::cout << newList[i] << "\n\n";
  51.             }
  52.  
  53.             //Clear vectors:
  54.             classList.clear();
  55.             newList.clear();
  56.         }
  57.         else {
  58.             std::cout << "\nPlease enter a list with an even number of names.\n\n";
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement