How can I call the elements present in the enum. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I call the elements present in the enum.

I want to cout the elements present in the enum using loop. For example. enum car{HONDA,TOYOTA,VOLVO,NISSAN,GM,FORD}; I want these names as out put not there indexes . As, HONDA TOYOTA... etc. Is that possible in C++?

3rd Oct 2020, 2:10 PM
Syed Muhammad Faqeeh Shah
Syed Muhammad Faqeeh Shah - avatar
5 Answers
+ 1
Syed Muhammad Faqeeh Shah I tried the std::map approach (code link added above). From the SO links above, which approach you prefer or maybe feel like implementing? I hope you'd also share your code, for someone who visits this thread one day.
4th Oct 2020, 4:58 AM
Ipang
+ 1
There is no easy way to do that, though, you can simulate it by using a std::map<car, std::string> where the key is one of the enum's constant, and the value is the string representation. But if you're desperate, you can check these related topics on SO: https://stackoverflow.com/questions/11714325/how-to-get-enum-item-name-from-its-value https://stackoverflow.com/questions/28828957/enum-to-string-in-modern-c11-c14-c17-and-future-c20 https://code.sololearn.com/cssX3mh2RrFl/?ref=app
3rd Oct 2020, 5:43 PM
Ipang
+ 1
Thanks Ipang😊, this link is a great help.👍
3rd Oct 2020, 9:01 PM
Syed Muhammad Faqeeh Shah
Syed Muhammad Faqeeh Shah - avatar
+ 1
Ipang Thanks again. I really appreciate that you shared your code. I will also share my code . If some needs my help in it.👍
4th Oct 2020, 5:02 AM
Syed Muhammad Faqeeh Shah
Syed Muhammad Faqeeh Shah - avatar
+ 1
Syed Muhammad Faqeeh Shah Yes, sharing is caring 😁👍
4th Oct 2020, 5:03 AM
Ipang