Conditional Statements

  • + 0 comments

    I know this is supposed to train your if/else conditons and the task has constraints by giving you input how it wants it to but solution like this could save you unnecessary lines of code:

    #include <iostream>
    #include <string>
    #include <array>
    
    int main() {
        std::string input;
        std::getline(std::cin, input);
        int n = std::stoi(input); // Assumes valid input per problem constraints
    
        constexpr std::array words = {"one", "two", "three", "four", "five", 
                                      "six", "seven", "eight", "nine"};
    
        if (n >= 1 && n <= 9) {
            std::cout << words[n - 1];
        } else if (n > 9) {
            std::cout << "Greater than 9";
        }
    
        return 0;
    }