testsAndMisc/CPP/miscelanious/tierListConverter/tierListConverter.cpp

76 lines
1.9 KiB
C++
Raw Normal View History

#include <iostream>
#include <string>
#include <vector>
2026-02-20 00:37:32 +01:00
const std::vector<std::string> TIERS = {"Abhorrent", "Bad", "Mid",
"Good", "Top", "God Tier"};
const float TIER_BASE = TIERS.size();
2026-02-20 00:37:32 +01:00
void print(std::string const s) { std::cout << s << std::endl; }
bool errorUserInput(std::string userInput) {
if (userInput.find("/") == std::string::npos) {
print("No '/' was found!");
return 1;
}
size_t positionOfSlash = userInput.find("/");
std::string nominatorS = userInput.substr(0, positionOfSlash);
try {
float nominator = stof(nominatorS);
} catch (std::invalid_argument) {
print("No number was found before the slash!");
return 1;
}
std::string denominatorS =
userInput.substr(positionOfSlash + 1, userInput.length() - 1);
try {
float denominator = stof(denominatorS);
if (denominator == 0) {
print("You cannot divide by 0!");
return 1;
}
} catch (std::invalid_argument) {
print("No number was found after the slash!");
return 1;
}
return 0;
}
2026-02-20 00:37:32 +01:00
std::string convertToTier(float nominator, float denominator) {
float fraction = nominator / denominator;
2026-02-20 01:17:53 +01:00
int tierIndex = 0;
2026-02-20 00:37:32 +01:00
for (int i = TIER_BASE; i > 0; i--) {
if (fraction >= (i / TIER_BASE)) {
tierIndex = i - 1;
break;
}
}
2026-02-20 01:17:53 +01:00
if (tierIndex == 0 && fraction > (1.1 / 10.0))
2026-02-20 00:37:32 +01:00
return TIERS[1];
return TIERS[tierIndex];
}
2026-02-20 00:37:32 +01:00
int main() {
std::string userScore;
do {
print("Enter your score in a format: numberOne/numberTwo");
getline(std::cin, userScore);
} while (errorUserInput(userScore));
2026-02-20 00:37:32 +01:00
size_t positionOfSlash = userScore.find("/");
std::string nominatorS = userScore.substr(0, positionOfSlash);
2026-02-20 00:37:32 +01:00
float nominator = stof(nominatorS);
std::string denominatorS =
userScore.substr(positionOfSlash + 1, userScore.length() - 1);
2026-02-20 00:37:32 +01:00
float denominator = stof(denominatorS);
print(convertToTier(nominator, denominator));
return 0;
}