c++arraysenumsstdstringstdarray

How to convert a string to map to its respective enum type integer?


I'm trying to create a program that will ask the user to enter an animal name and will print its respective age.

This is what I have so far, I'm using enum types and std::arrays to map easily it's values:

#include <iostream>
#include <string_view>  
#include <array>


namespace Animal
{
    enum Type
    {
        dog,  //0
        cat,  //1
        snake,  //2
        chicken,  //3
        max_animals, //4
    };


    //defining array of enumerator types “Type” 
    constexpr std::array <Type, max_animals> types {dog, cat, snake, chicken};


    //defining an array of string_view with the animal names
    using namespace std::string_view_literals;
    constexpr std::array <std::string_view, max_animals> names { "dog"sv, "cat"sv, "snake"sv, "chicken"sv }; //names


    //defining an array of integers with animal ages
    std::array age {8, 5, 11, 2}; //array of integers (implicitly deducted)


    static_assert(std::size(types) == max_animals);
    static_assert(std::size(names) == max_animals);
    static_assert(std::size(age) == max_animals);
}


int main()
{

    std::cout << "Enter an animal name to show their age  ";
    int x{};
    std::cin >> x; //i.e x = "snake"



//Conversion from string to its matching enum (i.e "snake" -> 2 integer)
    Animal::Type my_name{};  //initializing an enum type

    my_name = static_cast<Animal::Type>(x); //converting "snake"

    std::cout << my_name;


   //std::cout << "The age for " << Animal::names[ Animal::types[x] ] << " is " << Animal::age [ Animal::types[x] ];


    return 0;
}

I'm just a little stuck for hours in how to solve this. So I need to convert the string that was inputted into variable x, somehow to the appropiate enum number.

So that I can then use this number as an index to the age array.


EDIT: Develop a C++ program utilizing an unscoped enum type and std::array to facilitate a user input system. The program should prompt the user to enter a name, and based on this input, print the corresponding age.


Solution

  • Given the namespace Animal implementation, I think the easier way to achieve this is to input the animal's name as a string and find the index of that name in the names array. Here is your modified main function.

    int main()
    {
        std::cout << "Enter an animal name to show their age: ";
        std::string x;
        std::cin >> x; //i.e x = "snake"
    
        // Find the index of the animal's name in the names array
        auto it = std::find(Animal::names.begin(), Animal::names.end(), x);
    
        // If the animal's name was found in the array
        if (it != Animal::names.end())
        {
            // Get the index of the found animal name
            int index = std::distance(Animal::names.begin(), it);
    
            // Use the index to get the age of the animal
            std::cout << "The age of the " << Animal::names[index] << " is " << Animal::age[index] << '\n';
        }
        else
        {
            std::cout << "Animal not found\n";
        }
    
        return 0;
    }