I'm trying to do a try catch in line 21, so if the user types something other than 1, 2, or 3 it will give them an error. This is wrong, so how would I do this correctly?
Also, you will need to include the <stdexcept> library for std::out_of_range.
kbw wrote:
Now, that can be encapsulated in a function or a type, but that's the code that needs to be run to get that result.
To the OP: You can have a friend function (or just a function) which overloads the >> operator (std::cin >> type) that throws an error when the input is invalid.
1 2 3 4 5 6 7
std::istream& operator>>(std::istream& is, constclass_type& rhs)
{
// take input with is and throw error if input is invalid
// this function should be called within a try block and of course catch blocks should follow it
// or you could just ask the user to re-enter input in here
return is; // return std::cin at the end
}