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 | |
| parent | mergine develop after PR (diff) | |
| download | homework-1-arthurtspears-Homework2.tar.xz homework-1-arthurtspears-Homework2.zip | |
Started Homework2. Stubbed out main and worker threads. Organized other source files better.Homework2
Diffstat (limited to 'CST 126/Homework 1')
| -rw-r--r-- | CST 126/Homework 1/CST126_Structures.hpp | 67 | ||||
| -rw-r--r-- | CST 126/Homework 1/Homework 1.vcxproj | 4 | ||||
| -rw-r--r-- | CST 126/Homework 1/Homework 1.vcxproj.filters | 6 | ||||
| -rw-r--r-- | CST 126/Homework 1/helpers.hpp | 145 | ||||
| -rw-r--r-- | CST 126/Homework 1/main.cpp | 84 | ||||
| -rw-r--r-- | CST 126/Homework 1/menu.hpp | 27 | ||||
| -rw-r--r-- | CST 126/Homework 1/new_text.txt | 1 |
7 files changed, 270 insertions, 64 deletions
diff --git a/CST 126/Homework 1/CST126_Structures.hpp b/CST 126/Homework 1/CST126_Structures.hpp new file mode 100644 index 0000000..5a57b88 --- /dev/null +++ b/CST 126/Homework 1/CST126_Structures.hpp @@ -0,0 +1,67 @@ +#ifndef CST126_STRUCTURES_HPP +#define CST126_STRUCTURES_HPP + +#include <cstdint> +#include <ios> + +typedef unsigned long long uLLong; + +typedef unsigned char BYTE; + +constexpr size_t MAX_STREAM_SIZE = std::numeric_limits<std::streamsize>::max(); + +constexpr uLLong MAX_CHAR = 101; + +union FloatIntUnion +{ + float intFloat; + uint32_t uInt; +}; + +enum TemperatureType +{ + Fahrenheit, + Celsius +}; + +struct DailyTemperature +{ + float highTemp{}; + float lowTemp{}; + TemperatureType tempType{}; +}; + +enum DayOfTheWeek +{ + Sunday = 0, + Monday, + Tuesday, + Wednesday, + Thursday, + Friday, + Saturday = 6 +}; + +enum Month +{ + January = 1, + February, + March, + April, + May, + June, + July, + August, + September, + October, + November, + December +}; + +struct Date +{ + Month month; + uint8_t day; + uint16_t year; +}; +#endif diff --git a/CST 126/Homework 1/Homework 1.vcxproj b/CST 126/Homework 1/Homework 1.vcxproj index 553addf..3bd210f 100644 --- a/CST 126/Homework 1/Homework 1.vcxproj +++ b/CST 126/Homework 1/Homework 1.vcxproj @@ -136,6 +136,10 @@ <ClInclude Include="GuessingGame.hpp" /> <ClInclude Include="helpers.hpp" /> <ClInclude Include="menu.hpp" /> + <ClInclude Include="CST126_Structures.hpp" /> + </ItemGroup> + <ItemGroup> + <Text Include="new_text.txt" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> diff --git a/CST 126/Homework 1/Homework 1.vcxproj.filters b/CST 126/Homework 1/Homework 1.vcxproj.filters index a2e2650..b4b385f 100644 --- a/CST 126/Homework 1/Homework 1.vcxproj.filters +++ b/CST 126/Homework 1/Homework 1.vcxproj.filters @@ -29,5 +29,11 @@ <ClInclude Include="GuessingGame.hpp"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="CST126_Structures.hpp"> + <Filter>Header Files</Filter> + </ClInclude> + </ItemGroup> + <ItemGroup> + <Text Include="new_text.txt" /> </ItemGroup> </Project>
\ No newline at end of file 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 diff --git a/CST 126/Homework 1/main.cpp b/CST 126/Homework 1/main.cpp index e223d37..57c1541 100644 --- a/CST 126/Homework 1/main.cpp +++ b/CST 126/Homework 1/main.cpp @@ -8,56 +8,68 @@ #include "menu.hpp" #include <bitset> +#include "CST126_Structures.hpp" + using std::cout; using std::endl; -struct DailyTemperature +uLLong MAX_LENGTH = 101; + +struct Array { - int highTemp{}; - int lowTemp{}; + Array() + { + + } }; -enum DayOfTheWeek +void CharArrays() { - Sunday = 0, - Monday, - Tuesday, - Wednesday, - Thursday, - Friday, - Saturday = 6 -}; + char* inputChar = PromptInputNewCharArray("Input a string: ", MAX_LENGTH); + cout << inputChar; + delete[] inputChar; +} -enum Month +void Strings() { - January = 1, - February, - March, - April, - May, - June, - July, - August, - September, - October, - November, - December -}; + std::string myString = "Hello", mySecondString = " World"; + std::string thirdString = myString + mySecondString; + cout << thirdString; + + myString = PromptInputString("Input a new string: "); + cout << myString; +} -struct Date +void WriteToFileExample() { - Month month; - uint8_t day; - uint16_t year; -}; + char* outputToFile = PromptInputNewCharArray("Input to file:", MAX_LENGTH); -union FloatIntUnion + if (WriteToFile("new_text.txt", outputToFile)) + { + cout << "Write to file successful"; + } +} + +void ReadFromFileExample() { - float intFloat; - uint32_t uInt; -}; + if(ReadFromFileToConsole("new_text.txt")) + { + cout << "File read successfully" << endl; + } +} -int main() { +int main(int argc, char* argv[]) { + + int a = 5, b = 10; + bool condition = (a + b) < 15 && b > a; + + // Print the number of command line arguments + cout << "Number of arguments: " << argc << endl; + + // Loop through each argument + for (int i = 0; i < argc; ++i) { + cout << "Argument " << i << ": " << argv[i] << endl; + } return 0; } diff --git a/CST 126/Homework 1/menu.hpp b/CST 126/Homework 1/menu.hpp index 849f21a..c384927 100644 --- a/CST 126/Homework 1/menu.hpp +++ b/CST 126/Homework 1/menu.hpp @@ -8,7 +8,7 @@ using std::cout; using std::cin; using std::endl; -inline void DisplayMenu() { +inline void DisplayMenuHomework1Menu() { cout << "***************************************************************" << endl; cout << "Welcome to the menu!\n"; cout << "1. Currency Converter\n"; @@ -19,34 +19,9 @@ inline void DisplayMenu() { cout << "Please pick a number for your choice: "; } -inline void Worker() { - int input = 0; - DisplayMenu(); - cin >> input; - while (input != 5) - { - switch (input) { - case 1: - //Call Currency Function - break; - case 2: - //Call Guessing Game Function - OutputRandomNumber(); - break; - case 3: - //Call Temperature Logger - break; - default: - cout << "\nInvalid option, please pick again..\n"; - } - DisplayMenu(); - - cin >> input; - } -} #endif
\ No newline at end of file diff --git a/CST 126/Homework 1/new_text.txt b/CST 126/Homework 1/new_text.txt new file mode 100644 index 0000000..699b086 --- /dev/null +++ b/CST 126/Homework 1/new_text.txt @@ -0,0 +1 @@ +This will be the new content.
\ No newline at end of file |