/* testrep11.cpp CIS 150 6/10/2008 David Klick Data validation using a loop. Both a priming read and an alternative technique are used. */ #include using std::cin; using std::cout; void cont(); int main() { int length; // length of room int width; // width of room int area; // area of room // get length of room - priming read cout << "Enter length of room in inches (1 - 9999): "; cin >> length; // validate length - request again if invalid while (length<1 || length>9999) { cout << "Error: Invalid length.\n"; cout << "Enter length of room in inches (1 - 9999): "; cin >> length; } // get width of room - not priming read do { cout << "Enter width of room in inches (1 - 9999): "; cin >> width; if (width<1 || width>9999) { cout << "Error: Invalid width.\n"; } } while (width<1 || width>9999); // repeat if invalid // calculate and display area of room area = length * width; cout << "The area of the room is " << area << " square inches" << " (" << (area/144.0) << " square feet)\n"; cont(); return 0; } void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); } /* Sample output: Enter length of room in inches (1 - 9999): 246 Enter width of room in inches (1 - 9999): -10 Error: Invalid width. Enter width of room in inches (1 - 9999): 0 Error: Invalid width. Enter width of room in inches (1 - 9999): 12000 Error: Invalid width. Enter width of room in inches (1 - 9999): 180 The area of the room is 44280 square inches (307.5 square feet) */