// Zeinalipour-Yazti Demetrios // csyiazti // cs10xx - // Lab Sec#24 // lab6 /* Rectangle Area - WITH PROCEDURES & FUNCTIONS * 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 /* This function asks the user to input the value: valuename[] repetively until a * correct (>0) value is given. It then returns this value */ int getValue(char valuename[]) { /* defining local scope variables*/ int value; /* Getting the value */ while (true) //start while1a { cout << "\nPlease type in the (positive) \"" << valuename << "\" of the square: "; cin >> value; if (value>0) { break; } else { cout << "Error# - The value you entered is not a positive integer: " << endl; } } //end while1a return value; } /* This function calculates the area of the rectangle and returns that value */ int calculateArea(int length, int width) { return length*width; } /* This procedure simply outputs the area of the rectabgle along with the values of the 2 axis */ void printOutput(int length, int width, int area) { cout << "The area of the square (" << length << "x" << width << ") = " << area << endl << endl << endl; } /* This function asks the user if he wants to run the program again. * The function insists on asking until a valid answer is given ('y' OR 'n'). It thens * returns an aswer (true OR false) to this question. */ bool doCalculationAgain() { char quitquestion; /* Field to store users answer on quit question */ bool correctinput = false; /* Has the user entered a correct input */ bool quit = false; /* The question to be answered in this module */ /* 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 return quit; } void main() { /* define variables */ 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 */ /* define functions & procedures*/ int getValue(char valuename[]); /* returns the "correct' value given by the user */ int calculateArea(int length, int width); /* returns the area of the given rectangle */ void printOutput(int length, int width, int area); /* outputs area */ bool doCalculationAgain(); /* returns true if user if he wants to run program again */ /* */ while (!quit) //start while1 { /* get Values from user */ length = getValue("Length"); width = getValue("Width"); /* Calculating the square of the given values */ area = calculateArea(length,width); /* Printing out the result */ printOutput(length, width, area); /* Ask the user if he */ quit = doCalculationAgain(); } // end while1 }