Hi,
I'm trying to get my brain working again and running through a new (to me) book. One of the exercises is this:
Make a vector holding the ten string values "zero", "one", ... "nine".
Use that in a program that converts a digit to its corresponding spelled-Out
value; e.g., the input 7 gives the output seven. Have the same program,
using the same input loop, convert spelled-out numbers into their
digit form; e.g., the input seven gives the output 7.
It works fine, but it seems a bit long winded to me and I can't think of a way to really shorten it. I know I don't really need out_num (I was thinking to use it for the subscript originally), but I will need it for the next step that combines this with a calculator. I'd be glad to hear some suggestions.
Converting it the other way would be a bit more tricky.
I would suggest using a std::map<std::string, int> instead of a vector. Or even std::map<std::string, std::string> I suppose that way you could go both ways easier. http://www.cplusplus.com/reference/map/map/
It is a bit tricky having both the numbers and having them spelled out, if it was one or the other it would be simple. I've never use map, but this specifies using a vector so I'm pretty much stuck with that. The next part doesn't specify an output so I won't have to change it to its opposite anymore, just accept either as input. That should simplify it a bit since I can just convert the string to an int and go from there.