This exercise had me build on an earlier program - first ask user if wants to compute, then (if yes) choose circle or rectangle, then prompt for appropriate inputs, calculate, and run again.
I had a major brain freeze, such that this has taken me many hours just to get to this stage. It isn't quite finished but I'm stuck.
When I run this the program ignores the very first user input - should only be Y, y, N or n, and jumps right to the second prompt (type in circle or rectangle).
Can anyone tell me why?
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
char compute; // declare and initialize all integer variables to 0
bool response1 = false;
bool response2 = false;
bool response3 = false;
bool response4 = false;
int radius = 0;
int length = 0;
int width = 0;
int area = 0;
int perimeter = 0;
string shape;
const string circle ("circle");
const string rectangle ("rectangle");
const double PI = 3.14159;
cout << "This program will calculate the area and perimeter" << endl; // tell the user what the program will do
cout << "of either a circle or a rectangle." << endl;
cout << endl << endl;
cout << "Do you wish to compute the area and perimeter of a shape?" << endl; // prompt the user to select to continue or not
cout << "Please type in a 'Y' or a 'y' for yes or an 'N' or an 'n' for no." << endl;
cout << "Then press enter." << endl;
cout << endl;
cin >> compute;
cout << endl;
while (!response1) // create loop to ensure proper response to compute question
{
if ((compute = 'Y') || (compute = 'y'))
{
response1 = true;
cout << "Please type in 'circle' or 'rectangle' and press enter." << endl;
cin >> shape;
while (!response2) // create loop to ensure proper response to shape question
{
if (shape.compare(circle) == 0)
{
response2 = true;
cout << "Please type in a positive radius, in inches, of the circle " << endl; // prompt for user to input radius
cout << "then push the enter key" << endl;
cout << endl;
cin >> radius;
while (!response3) // create loop to ensure propre input for radius
{
if (radius > 0) // if "radius" is in proper format, set flag to exit loop, calculate are and perimeter, and print out
{
response3 = true;
area = PI * radius * radius;
perimeter = 2 * PI * radius;
cout << endl;
cout << "The area of the " << shape << " is " << area << " square inches" <<endl; // output appropriate answers to users inputs
cout << "The perimeter of the " << shape << " is " << perimeter << " linear inches" <<endl;
cout << endl;
area = 0; // reset variables to zero
perimeter = 0;
}
else // if "radius" in not in proper format, prompt user to try again. will now go back to start of loop
{
cout << "You input an improper number" << endl;
cout << "Please try again" << endl;
cout << "Please type in a positive radius, in inches, of the circle " << endl; // prompt for user to input radius
cout << "then push the enter key" << endl;
cout << endl;
cin >> radius;
}
}
}
else if (shape.compare(rectangle) == 0) // default is rectangle
{
response2 = true;
cout << "Please type in a positive length, in inches, of one side of the rectangle" << endl;
cout << "then push the enter key" << endl;
cout << endl;
cin >> length;
cout << endl;
cout << "Now please type in a positive width, in inches, of one side of the rectangle" << endl;
cout << "then push the enter key" << endl;
cout << endl;
cin >> width;
while (!response4)
{
if ((length > 0) && (width > 0)) // if both "length" and "width" are both in proper format ( > zero) - set flags to exit loop, calculate area and perimeter and print out
{
response4 = true;
area = width * length;
perimeter = 2 * (width + length);
cout << endl;
cout << "The area of the " << shape << " is " << area << " square inches" <<endl; // output appropriate answers to users inputs
cout << "The perimeter of the " << shape << " is " << perimeter << " linear inches" <<endl;
cout << endl;
length = 0; // set relevant variables back to zero
width = 0;
}
else // if "length" && "width" not in proper format, prompt user to try again. will now go back to start of loop
{
cout << "The number you input for length or width, or both, are incorrect" << endl;
cout << "Please try again" << endl;
}
}
}
else
{ // if response is neither circle or rectangle do not drop out of loop but ask again
cout << "Your response is not in the correct format" << endl;
cout << "Please try again" << endl;
cout << "Please type in 'circle' or 'rectangle' and press enter." << endl;
cin >> shape;
}
}
}
else if ((compute = 'N') || (compute = 'n')) // advise user that program will exit
{
response1 = true;
cout << "You have chosen to not continue." << endl;
cout << "The program will now exit" << endl;
}
else
{ // if response is neither compute nor not compute do not drop out of loop but ask again
cout << "Your response is not in the correct format" << endl;
cout << "Please try again" << endl;
cout << "Do you wish to compute the area and perimeter of a shape?" << endl;
cout << "Please type in a 'Y' or a 'y' for yes or an 'N' or an 'n' for no." << endl;
cout << "Then press enter." << endl;
cout << endl << endl;
cin >> compute;
}
}
return 0;
}