diff options
| author | Arthur Spears <[email protected]> | 2024-04-25 20:19:57 -0700 |
|---|---|---|
| committer | Arthur Spears <[email protected]> | 2024-04-25 20:19:57 -0700 |
| commit | d65138f2e040fe4193ea3fb4e2908bd0b161aadd (patch) | |
| tree | 821fc4ca98777efd74334829599ea0876fe79b08 /CST 126/Homework 1/helpers.hpp | |
| parent | mergine develop after PR (diff) | |
| download | archived-homework-1-arthurtspears-d65138f2e040fe4193ea3fb4e2908bd0b161aadd.tar.xz archived-homework-1-arthurtspears-d65138f2e040fe4193ea3fb4e2908bd0b161aadd.zip | |
Started Homework2. Stubbed out main and worker threads. Organized other source files better.Homework2
Diffstat (limited to 'CST 126/Homework 1/helpers.hpp')
| -rw-r--r-- | CST 126/Homework 1/helpers.hpp | 145 |
1 files changed, 143 insertions, 2 deletions
diff --git a/CST 126/Homework 1/helpers.hpp b/CST 126/Homework 1/helpers.hpp index f34a66d..d2d52d2 100644 --- a/CST 126/Homework 1/helpers.hpp +++ b/CST 126/Homework 1/helpers.hpp @@ -2,9 +2,13 @@ #define HELPERS_HPP #include <random> +#include <string> +#include <iostream> +#include <fstream> -typedef unsigned long long uLong; -typedef unsigned char BYTE; +using std::cin; +using std::cout; +using std::endl; inline int Random(const int& lowest, const int& highest) { @@ -26,6 +30,7 @@ inline void GenerateRandomNumbers(int arrayToFill[], const int& size) arrayToFill[i] = random_number; } } + inline int add(int num, char myChar) { return num + myChar; @@ -35,11 +40,147 @@ 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 |