I am new in C++ and came up with the idea of writing "a short quiz" to use "enum" and have a clearer understanding of how it works. My idea consists in displaying three options to the user, and the he/she would have to introduce the number that corresponds to the correct option. This depicts the situation:
# include <iostream>
int main(){
std::cout << "Which one is the capital of Ireland?";
std::cout << "\n1-Barcelona\n2-Frankfurt\n3-Dublin\n";
std::cout << "Please introduce the number of the correct option";
enum question {barcelona=1,frankfurt=2,dublin=3};
int iAnswer;
std::cin >> iAnswer;
question eAnswer= static_cast<question>(iAnswer);
The problem with this code is that after casting the integer introduced by the user (iAnswer), there's no error message/warning when such number does not exist in the "question" data type options. In other words; if the user introduced 100, after casting iAnswer we could see that there's no option related to 100 since the only accepted values are 1,2 and 3. The question would be then... how could I check that the number the user introduces does exist in my enum options? why casting doesn't show any error when in theoretically the casting couldn't be successfully done?
Thanks!!