#ifndef HELPERS_HPP #define HELPERS_HPP #include #include #include #include using std::cin; using std::cout; using std::endl; inline int Random(const int& lowest, const int& highest) { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution dis(lowest, highest); const int random_number = dis(gen); return random_number; } inline void GenerateRandomNumbers(int arrayToFill[], const int& size) { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution dis(0, size); for (auto i = 0; i < size; ++i) { const int random_number = dis(gen); arrayToFill[i] = random_number; } } inline int add(int num, char myChar) { return num + myChar; } inline int add(int num, int num2, int num3) { return num + num2 + num3; } inline int add(int num, int num2, int num3, int num4) { return num + num2 + num3 + num4; } inline int ReadInt(const char* prompt) { using std::cin; using std::cout; using std::endl; int intVal; cout << endl << prompt; cin >> intVal; while (!cin) { cin.clear(); cin.ignore(MAX_STREAM_SIZE, '\n'); cout << "Invalid integer. Please try again: "; cout.flush(); cin >> intVal; } cin.ignore(MAX_STREAM_SIZE, '\n'); return intVal; } // Improved function for reading strings with user input inline char* PromptInputNewCharArray(const char* prompt, long long maxLen) { char* inputStr = nullptr; try { inputStr = new char[maxLen]; } catch (const std::exception& ex) { delete[] inputStr; inputStr = nullptr; std::cerr << "Unable to allocate new char array: " << ex.what(); return inputStr; } cout << endl << prompt; // Ensure the prompt is displayed immediately cout.flush(); cin.get(inputStr, maxLen, '\n'); while (!cin) { // Clear error flag cin.clear(); // Ignore all characters until a newline cin.ignore(MAX_STREAM_SIZE, '\n'); cout << endl << prompt; // Flush again to ensure prompt is visible cout.flush(); cin.get(inputStr, maxLen, '\n'); } // Discard any characters remaining in the buffer, including the newline cin.ignore(MAX_STREAM_SIZE, '\n'); return inputStr; } inline std::string PromptInputString(const char* prompt) { std::string line; cout << endl << prompt; // Ensure the prompt is displayed immediately cout.flush(); std::getline(cin, line); while (!cin) { // Flush again to ensure prompt is visible cout.flush(); // Clear error flag cin.clear(); cout << endl << prompt; std::getline(cin, line); } return line; } inline bool ReadFromFileToConsole(const char* fileName) { std::ifstream file(fileName); if (!file.is_open()) { std::cerr << "Could not open file: " << fileName; return false; } std::string line; while (getline(file, line)) { std::cout << line << std::endl; //does something with the reading of file } file.close(); return true; } inline bool WriteToFile(const char* fileName, const char* fileContents) { std::ofstream file(fileName); if (!file.is_open()) { std::cerr << "Could not open file: " << fileName; return false; } file << fileContents; file.close(); return true; } #endif