C++ String to Integer Conversion

For me looks good. I have tested it on my MX Linux distribution.

It’s interesting how much the code says about it’s author. In my case 7 years in Elixir gives result …

  1. Even if somehow I remember somewhat about try/catch I was still looking for a “better” solution, so I have added one extra dependency for a single isdigit call. :sweat_smile:

  2. std:err of course! When I saw your code I have reminded it immediately. :older_man:

  3. stdexcept is interesting. Maybe I have used it, but I don’t remember it much especially e.what() call. I guess that I haven’t used multiple catch blocks before in C++. :bulb:

  4. out_of_range error handling is simple, but also brilliant idea. I’m not surprised that I wasn’t thinking about such edge case since in Elixir we have just Integer for all well … integers (no small or big ones). :+1:

  5. I only wonder why you define num variable outside of try block having in mind it’s only usage is within it, but that’s definitely not the most important thing. :thinking:

So much to learn in such a small code, thanks! :open_book:

Adding additional links below to the documentation pages:

  1. Reference : stdexcept : invalid_argument
  2. Reference : stdexcept : out_of_range
2 Likes