0

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();

}
  • 1
    When I enter 8* as the input, the program prints `heredone!`. I don't see any reason to expect an error. `try` -> `while` -> `if` -> `if` -> `break` – lakshayg Aug 31 '18 at 22:54
  • C++ I/O errors don't throw exceptions, unless you explicitly enable them. And really, if you want to learn to program, don't learn from Stroustrup's book - he is not a good teacher, and arguably don't learn C++ as your first language - learn Python. –  Aug 31 '18 at 22:55
  • @LakshayGarg First part was corrected. For the second part of your answer, we want to enter a number between 1 to 10 but 8* is not a number between 1 to 10 but this program accepts it. How can I modify my program to get a correct result? – Amir Tavakkoli Aug 31 '18 at 23:07
  • @AmirTavakkoli Have a look at https://stackoverflow.com/questions/15594995/how-to-make-cin-not-convert-float-to-integer this is about a problem very similar to yours. – lakshayg Aug 31 '18 at 23:14
  • @NeilButterworth Thanks for your comment. I'm not completely new to programming. I have programmed with phyton but i didn't get to the details of phyton and now I want to study c++ in full detail. what do you suggest for studying c++? – Amir Tavakkoli Aug 31 '18 at 23:17
  • @LakshayGarg Thanks! – Amir Tavakkoli Aug 31 '18 at 23:20
  • You can read a string instead. Then [this post](https://stackoverflow.com/q/32991193/5376789) may be helpful. – xskxzr Sep 01 '18 at 09:13

0 Answers0