/* * example based on R. Grimm, C++ Core Guidelines Explained, p. 443. */ #include #include namespace { const int lineskip_characters = 255; } int area(int height) { Expects(height > 0); // *** here we check a precondition *** std::cout << "Input a natural number: width = "; int width = 0; std::cin >> width; std::cin.ignore(lineskip_characters, '\n'); // ignore rest of the input int retv = height*width; Ensures(retv > 0); // *** here we check a postcondition *** return retv; } int main() { std::cout << "Input a natural number: height = "; int h = 0; std::cin >> h; std::cin.ignore(lineskip_characters, '\n'); // ignore rest of the input Ensures(h > 0); // *** here we check a run-time condition *** int a = area(h); std::cout << "Product: area = " << a << "\n"; }