summaryrefslogtreecommitdiff
path: root/BlankConsoleLab
diff options
context:
space:
mode:
authorJoseph Williams <[email protected]>2022-11-09 00:44:16 -0800
committerJoseph Williams <[email protected]>2022-11-09 00:44:16 -0800
commit50cb713219dc4e1ca61db20c74f92bfe0d39acda (patch)
tree8a25840835c0ebd79f78e0e41cfc211211e2de4f /BlankConsoleLab
parentfinished user input functions (diff)
downloadcst116-lab2-allthenamesaretaken3141-50cb713219dc4e1ca61db20c74f92bfe0d39acda.tar.xz
cst116-lab2-allthenamesaretaken3141-50cb713219dc4e1ca61db20c74f92bfe0d39acda.zip
finished writing code. have not tested anything because it's currently 12:43 am and i have to wake up at like 7 tomorrow
Diffstat (limited to 'BlankConsoleLab')
-rw-r--r--BlankConsoleLab/CST116_Lab2_Williams.cpp84
1 files changed, 73 insertions, 11 deletions
diff --git a/BlankConsoleLab/CST116_Lab2_Williams.cpp b/BlankConsoleLab/CST116_Lab2_Williams.cpp
index 4a39cfc..e622b07 100644
--- a/BlankConsoleLab/CST116_Lab2_Williams.cpp
+++ b/BlankConsoleLab/CST116_Lab2_Williams.cpp
@@ -5,11 +5,12 @@
#include <algorithm>
#include <string>
#include <sstream>
+#include <math.h>
using namespace std;
-// Printing/input functions that I shamelessly stole from myself. Nothing new here. The pragma command isn't c++ code and is only there so that visual studio collapses all these functions down to one line.
-#pragma region colorprints
+// Printing functions that I shamelessly stole from myself. The pragma command isn't c++ and is only there so that visual studio collapses the entire section down to one line and makes it look like my code isn't incredibly bloated and inefficient.
+#pragma region output
const HANDLE HCONSOLE = GetStdHandle(STD_OUTPUT_HANDLE);
// Uses black magic to print in full color with (optional) automatic line breaks. Requires <windows.h>. Returns nothing and takes up to 4 parameters:
@@ -45,8 +46,10 @@ void multiColorPrint(vector<tuple<string, int, bool> > text, HANDLE console = HC
colorPrint(fragmentText, fragmentColor, fragmentBreak, console); //Print the current block.
}
}
-#pragma endregion colorprints
+#pragma endregion
+// Input functions.
+#pragma region input
// It turns out that cin will usually break when you input a string that has whitespace in it, so I wrote this to fix it. Feel free to stick with cin if you're not trying to get input with whitespace, though.
string whitespaceInput()
{
@@ -101,7 +104,7 @@ bool isNumber(string str) {
// 0: The user entered the temperature in F
// 1: The user entered the temperature in C
float getTemp(int& unit) {
- vector <tuple<string, int, bool> > asktext = {make_tuple("Please enter the current temperature followed by its unit (", 15, 0), make_tuple("[F]ahrenheit", 9, 0), make_tuple(" or ", 15, 0), make_tuple("[C]elcius", 9, 0), make_tuple("): ", 15, 0)};
+ vector <tuple<string, int, bool> > asktext = {make_tuple("Please enter the current temperature followed by its unit (", 15, 0), make_tuple("[F]ahrenheit", 11, 0), make_tuple(" or ", 15, 0), make_tuple("[C]elcius", 11, 0), make_tuple("): ", 15, 0)};
float temp;
while (true)
{
@@ -141,7 +144,7 @@ float getTemp(int& unit) {
// 0: The user entered the wind speed in mph
// 1: The user entered the wind speed in kph
float getWind(int& unit) {
- vector <tuple<string, int, bool> > asktext = { make_tuple("Please enter the current wind speed followed by its unit (", 15, 0), make_tuple("miles per hour", 9, 0), make_tuple(" or ", 15, 0), make_tuple("kilometers per hour", 9, 0), make_tuple("): ", 15, 0) };
+ vector <tuple<string, int, bool> > asktext = { make_tuple("Please enter the current wind speed followed by its unit (", 15, 0), make_tuple("miles per hour", 11, 0), make_tuple(" or ", 15, 0), make_tuple("kilometers per hour", 11, 0), make_tuple("): ", 15, 0) };
float wind;
string input;
while (true)
@@ -206,17 +209,76 @@ float getWind(int& unit) {
}
}
}
+#pragma endregion
+
+// Unit conversion/calculation functions.
+#pragma region computation
+// Temperature/wind speed unit converters.
+float dctodf(float t) {
+ return t * 9 / 5 + 32;
+}
+
+float dftodc(float t) {
+ return (t - 32) * 5 / 9;
+}
+
+float kphtomph(float s) {
+ return s / 1.609;
+}
+
+float mphtokph(float s) {
+ return s * 1.609;
+}
+
+
+float windChill(float t, float w) {
+ return 35.74 + 0.6215 * t - 35.75 * pow(w, 0.16) + 0.4275 * t * pow(w, 0.16);
+}
+#pragma endregion
+
+
int main()
{
int tempUnit, windUnit;
- float temp = getTemp(tempUnit);
- string tempName; tempUnit == 0 ? tempName = "Fahrenheit" : tempName = "Celcius";
- cout << temp << " " << tempName << endl;
+ float temp, wind, fTemp, cTemp, mphWind, kphWind, fChill, cChill;
+
+ // Get temperature and wind speed from the users.
+ temp = getTemp(tempUnit);
+ wind = getWind(windUnit);
+
+ // Get values for both Fahrenheit and Celcius (and put them in the right places).
+ if (tempUnit == 0)
+ {
+ fTemp = temp;
+ cTemp = dftodc(temp);
+ }
+ else
+ {
+ fTemp = dctodf(temp);
+ cTemp = temp;
+ }
+
+ // Do the same thing for wind speed.
+ if (windUnit == 0)
+ {
+ mphWind = wind;
+ kphWind = mphtokph(wind);
+ }
+ else
+ {
+ mphWind = kphtomph(wind);
+ kphWind = wind;
+ }
+
+ // Calculate the wind chill. The wind chill calculator uses Imperial units and I just convert it to Celcius.
+ fChill = windChill(fTemp, mphWind);
+ cChill = dftodc(fChill);
- float wind = getWind(windUnit);
- string windName; windUnit == 0 ? windName = "miles per hour" : windName = "kilometers per hour";
- cout << wind << " " << windName << endl;
+ // Print all the output, using needlessly complicated functions that I'm too stubborn to refine.
+ vector <tuple <string, int, bool> > tempOut = { make_tuple("The temperature is ", 15, 0), make_tuple(to_string(fTemp) + " degrees Fahrenheit", 11, 0), make_tuple(", or ", 15, 0), make_tuple(to_string(cTemp) + " degrees Celcius", 11, 0), make_tuple(".", 15, 0), };
+ vector <tuple <string, int, bool> > windOut = { make_tuple("The wind speed is ", 15, 0), make_tuple(to_string(mphWind) + " miles per hour", 11, 0), make_tuple(", or ", 15, 0), make_tuple(to_string(kphWind) + " kilometers per hour", 11, 0), make_tuple(".", 15, 0), };
+ vector <tuple <string, int, bool> > chillOut = { make_tuple("The wind chill is ", 15, 0), make_tuple(to_string(fChill) + " degrees Fahrenheit", 11, 0), make_tuple(", or ", 15, 0), make_tuple(to_string(cChill) + " degrees Celcius", 15, 0), make_tuple(".", 15, 0), };
return 0;
}