#include <iostream>
using namespace std;
template<typename T = int> T thresholdOrDefault(T v, T def = {}) {
if (v < 0) return def;
return v;
}
int main() {
int v, def;
if (cin.peek() == '-') {
cin >> v;
if (cin.peek() == ' ') {
cin >> def;
cout << thresholdOrDefault(v, def) << "\n";
} else {
cout << thresholdOrDefault(v) << "\n";
}
} else {
cin >> v;
cout << thresholdOrDefault(v) << "\n";
}
return 0;
}
Solution Details
👉 In simple words:
If the input value is invalid (negative), the function falls back to a default. If it’s fine, the function just returns it.
Significance for Embedded Developers
Input
5
Expected Output
5