Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <cstdlib>
- #include <ctime>
- #include <fstream>
- int main()
- {
- //Define variables:
- std::vector<std::string> classList;
- std::vector<std::string> newList;
- std::string file = "";
- std::ifstream myFile(file);
- std::string line = "";
- int count = 0;
- //Main loop:
- while (file != "exit") {
- std::cout << "Enter file directory (type 'exit' to end process): ";
- std::cin >> file;
- //Add names from file to classList:
- if (myFile.is_open()) {
- while (std::getline(myFile, line)) {
- classList.push_back(line);
- count++;
- }
- }
- //Ensure the list has an even number of names:
- if (count % 2 == 0 && file != "exit") {
- std::srand(unsigned(std::time(0)));
- //Randomize the classList:
- std::random_shuffle(classList.begin(), classList.end());
- //Pair the names that are next to each other and add it to the newList:
- for (int i = 0; i < classList.size() - 1; i++) {
- std::string str1 = classList[i] + "\n" + classList[i + 1];
- i++;
- newList.push_back(str1);
- }
- //Print out pairs:
- std::cout << "\nRandomly generated pairs:\n\n";
- for (int i = 0; i < newList.size(); i++) {
- std::cout << newList[i] << "\n\n";
- }
- //Clear vectors:
- classList.clear();
- newList.clear();
- }
- else {
- std::cout << "\nPlease enter a list with an even number of names.\n\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement