I'm new to programming. This is a program from Programming: Principles and Practice Using C++ book but with a little adjustment for the standard library. In this program when I enter 8* for my input suddenly everything closes without any exception throwing. What's the flow of the program that doesn't throw any exception?
Here's the code:
#include <iostream>
#include <string>
using namespace std;
inline void open() {
cin.clear();
cin.ignore();
cin.get();
}
void skip_to_int()
{
if (cin.fail()) { // we found something that wasn’t an integer
cin.clear(); // we’d like to look at the characters
for (char ch; cin >> ch; ) { // throw away non-digits
if (isdigit(ch) || ch == '-') {
cin.unget(); // put the digit back,
// so that we can read the number
return;
}
}
}
throw runtime_error("no input"); // eof or bad: give up
}
int main(){
try {
cout << "Please enter an integer in the range 1 to 10 (inclusive):\n";
int n = 0;
while (true) {
if (cin >> n) { // we got an integer; now check it
if (1 <= n && n <= 10) {
break;
}
cout << "Sorry " << n
<< " is not in the [1:10] range; please try again\n";
}
else {
cout << "Sorry, that was not a number; please try again\n";
skip_to_int();
}
}
}
catch (runtime_error& e) {
cerr << e.what() << endl;
}
open();
}