Skip to content

Instantly share code, notes, and snippets.

@Keating950
Created September 15, 2021 22:06
Show Gist options
  • Save Keating950/a9143a115e565090b1ec9c919d43f994 to your computer and use it in GitHub Desktop.
Save Keating950/a9143a115e565090b1ec9c919d43f994 to your computer and use it in GitHub Desktop.
diff --git a/baby_first.cpp b/baby_second.cpp
index 08796aa..746f847 100644
--- a/baby_first.cpp
+++ b/baby_second.cpp
@@ -24,37 +24,24 @@ void cartProduct(std::set<int> const &n, std::set<float> const &b1, Iterator out
}
template<typename T>
-void strToType(std::string const &str, T *value, int range) {
+T strToType(std::string const &str, int range) {
// takes in string, transforms to type 'T' and checks whether range is met.
- if (typeid(T) == typeid(int)) {
- *value = std::stoi(str);
- } else if (typeid(T) == typeid(float)) {
- *value = std::stof(str);
- } else {
- throw std::bad_typeid();
- }
-
- if (*value > 0 && range > 0 || *value >= 0 && range == 0 || range < 0) return;
+ T value = (typeid(T) == typeid(int)) ? std::stoi(str) :
+ (typeid(T) == typeid(float)) ? std::stof(str) :
+ throw std::bad_typeid();
+ if (value > 0 && range > 0 || value == 0 && range == 0 || range < 0)
+ return;
throw std::out_of_range("One or more arguments did not follow the range specifications.");
}
template<typename T>
void parse_params(std::string str, std::vector<T> *result, int range) { // Here
// Takes in user entered parameters, cleans them, then outputs 2D array of numeric parameters.
- T temp;
-
while (!empty(str)) {
std::size_t found = str.find(',');
- if (found != std::string::npos) {
- strToType<T>(str.substr(0, found), &temp, range);
- result->push_back(temp);
- str = str.substr(++found);
- } else {
- strToType<T>(str.substr(0, found), &temp, range);
- result->push_back(temp);
- str = "";
- }
+ result->push_back(strToType<T>(str.substr(0, found), range));
+ str = found == std::string::npos ? "" : str.substr(++found);
}
}
@@ -73,8 +60,8 @@ int main() {
std::cout << "Beta 1 (First Parameter): ";
getline (std::cin, entBOne);
try {
- parse_params<int>(entSizes, &sampleSizes, 1); // The address of this thing is sent to...
- parse_params<float>(entBOne, &betaOne, -1);
+ sampleSizes = parse_params<int>(entSizes, 1); // The address of this thing is sent to...
+ betaOne = parse_params<float>(entBOne, -1);
}
catch (std::invalid_argument& e) {
std::cout << "Error in parsing your arguments: Please only use numerals and commas in input line." << std::endl;
@@ -96,7 +83,7 @@ int main() {
timeinfo = localtime(&rawtime);
// Ask whether random should be seeded (for testing!)
- char seed;
+ unsigned seed;
do {
std::cout << "Would you like to seed random? [y/n]: ";
std::cin >> seed;
@@ -141,4 +128,4 @@ int main() {
std::cout << "(" << std::get<0>(v) << "," << std::get<1>(v) << ") ";
}
return 0;
-}
\ No newline at end of file
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment