// Zeinalipour-Yazti Demetrios // csyiazti // cs10xx - // Lab Sec#24 // lab4 /* * This program allows the user to calculate the area of a rectangle many times. * A) Input: length, width, continue/quit * B) Restrictions: inputs must be positive * C) Output: area * D) Formula: area = length * width * */ #include void main() { int length; /* The length of the square */ int width; /* The width of the square */ int area; /* The area of the square */ bool quit = false; /* Should the program stop executing */ char quitquestion; /* Field to store users answer on quit question */ bool correctinput = false; /* Has the user entered a correct input */ /* */ while (!quit) //start while1 { /* Getting the length */ while (true) //start while1a { cout << "\nPlease type in the (positive) \"length\" of the square: "; cin >> length; if (length>0) { break; } else { cout << "Error# - The value you entered is not a positive integer: " << endl; } } //end while1a /* Getting the width */ while (true) { cout << "Please type in the (positive) \"width\" of the square: "; cin >> width; if (width>0) { break; } else { cout << "Error# - The value you entered is not a positive integer!\n\n"; } }//end while1b /* Calculating the square of the given values */ area = length*width; /* Printing out the result */ cout << "The area of the square (" << length << "x" << width << ") = " << area << endl << endl << endl; /* Asking if the user want to execute again */ while (!correctinput) { cout << "Do you want to do the calculation again? (y/n): "; cin >> quitquestion; switch (quitquestion) { case 'y' : correctinput = true; break; case 'n' : quit = true; correctinput = true; break; default: cout << "Error# - Please use \"y\" for yes OR \"n\" for no!\n\n"; } }//end while1b correctinput = false; /* Setting the variable to false again - reuse it*/ } // end while1 }