Program to take a number from 0 to 9 as input
This can be rewritten using regular expression \s*(\d)\D* (zero or more whitespace characters, a single digit, zero or more non digit characters. If I understand correctly, heading and trailing spaces are ok, 2ff is ok but ff2 is not ok and 23 is not ok either.
int input_number() {
  static const std::regex valid_number(&quot;\\s*(\\d)\\D*&quot;);
  string data;
  std::smatch matches;
  while (true) {
    cout &lt;&lt; &quot;Input: &quot;;
    if (getline(cin, data) &amp;&amp; 
          std::regex_match(data, matches, valid_number)) {
        return stoi(matches[1]);
    }
    cerr &lt;&lt; &quot;Invalid number, try again!&quot; &lt;&lt; endl;
  }
}

The exception on parsing should never be thrown, regular expression takes care to feed only valid number as the input. The initial version contained try/catch construct to catch it anyway and assert false but this idea is probably over-engineering.