/* * example based on R. Grimm, C++ Core Guidelines Explained, p. 443. */ #include #include namespace { const int lineskip_characters = 255; } int area(int height) { assert(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'); int retv = height*width; assert(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 assert(h > 0); // *** here we check a run-time condition *** int a = area(h); std::cout << "Product: area = " << a << "\n"; }