Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I wrote program that can calculates different shapes, but I have troubles. The p

ID: 3539375 • Letter: I

Question

I wrote program that can calculates different shapes, but I have troubles. The program goes like this: The user select the shape he/she wants and once the user selects a shape, then the user will get the area; furthermore, the program will ask if you want another selection, then the user will type Y (yes) or N (no); if the user says Yes, then the user selects another shape, but if the user says no, then the program will exit saying "Thank you for your time."


Problem: When the user type Yes, it gives an infinite loop and when the user says no, it gives infinite loop as well.


My Codes: http://www.mediafire.com/folder/9po5nj94r059m/Shapes4


Any solutions?

Explanation / Answer

//File: shapes3.cpp


#include<iostream>

#include<cstdlib>

using namespace std;

class Shapes

{

private:

char selection;

public:

char SelectShape();

void DisplayMenu();

void GetSelection();

void CheckSelection();

int GetInteger();

void Rectangle();

void Triangle();

void Trapezoid();

void Circle();

void ImplementShape();

bool CheckContinue();

};




int SelectShape(char);

int bye = 0;

char selection;


void Shapes::DisplayMenu()

{

//int bye = 0;

while (bye == 0)

{

cout << " ________________________" << endl;

cout << "|Area Calculation Program|" << endl;

cout << "|------------------------|" << endl;

cout << "| <R>ectangle |" << endl;

cout << "| <T>rianle |" << endl;

cout << "| Trape<Z>oid |" << endl;

cout << "| <C>ircle |" << endl;

cout << "| <E>xit Program |" << endl;

cout << "|________________________|" << endl;

cout << "|Select Shape: |"<< endl;

cout << "|________________________|" << endl;

cout << ' ' << endl;

cout << "Shape Selected: ";

cin >> selection;

ImplementShape();

}

}


void Shapes::GetSelection()

{

selection = cin.get();

cin.ignore(10, ' ');

}


void Shapes::CheckSelection()

{

selection = toupper(selection);

cout << "You've entered: " << selection << endl;

if((selection != 'R')&&(selection !='T')&&(selection != 'Z')&&(selection != 'C')&&(selection != 'E'))

selection = 'I';//Invalid Selection

}


int Shapes::GetInteger()//Needed for Rectangle() and Triangle()

{

int i;

do

{

cout << " Positive integer value: ";

cin>>i;

cin.ignore(10, ' ');


if (!cin.good())

{

cin.clear();

cin.ignore(10, ' ');

cout << " Incorrect input: negative integer. ";

continue;

}

else if (i>0)

return i;

else

{

cout << " Incorrect input : negative integer. ";

continue;

}

}while(true);

}


void Shapes::Rectangle()

{

cout << endl;

cout << " ^--------------- ";

cout << " || | ";

cout << " a| | ";

cout << " || | ";

cout << " v--------------- ";

cout << " <------b-------> ";


cout << " Side a:";

int a = GetInteger();


cout << " Side b:";

int b = GetInteger();


cout << "Area of Rectangle is " << a * b << endl;

}


void Shapes::Triangle()

{

cout << endl;

cout << "^|\ ";

cout << "|| \ ";

cout << " | \ ";

cout << "a| \ ";

cout << " | \ ";

cout << "|| \ ";

cout << "v| \ ";

cout << " -------- ";

cout << " <---b---> ";




cout << " Side a: ";

int a = GetInteger();


cout << " Side b: ";

int b = GetInteger();


cout << "Area of Triangle is " << 0.5 * a * b << endl;

}

void Shapes::Trapezoid()

{

int b1, b2, h;//h = height

cout << endl;

cout << " <-b1->" << endl;

cout << " ------" << endl;

cout << " / ^ \" << endl;

cout << " / h \" << endl;

cout << "/ v \" << endl;

cout << "-------------" << endl;

cout << "<---- b2 --->" << endl;


cout << " Enter side b1: ";

cin >> b1;

cout << "Enter side b2: ";

cin >> b2;

cout << "Enter the height: ";

cin >> h;

cout << " Trapezoid area: " << ((b1 + b2)/2) * h << endl;

}


void Shapes::Circle()

{

int r;// radius

double pi = 3.14159265359;

cout << endl;

cout << " ** " << endl;

cout << " * *" << endl;

cout << " * *" << endl;

cout << "* *" << endl;

cout << "* ---r---*" << endl;

cout << "* *" << endl;

cout << " * *" << endl;

cout << " * *" << endl;

cout << " ** " << endl;


cout << " Enter the radius: ";

cin >> r;

cout << " Area of Circle: "<< 2 * pi * r << endl;

}


void Shapes::ImplementShape()

{

bye = 0;

switch(selection)

{

case 'R':

Rectangle();

break;

case 'T':

Triangle();

break;

case 'Z':

Trapezoid();

break;

case 'C':

Circle();

break;

case 'E':

cout << "Thank you for your time! :)" << endl;

bye = -1;

exit(0);

break;

default:

cout << " Please input valid selection: " << endl;

break;

}

if(bye==0)

bye = CheckContinue();

}

bool Shapes:: CheckContinue()

{

char x;

int flag=0;

cout << " Do you want to continue with another selection? "

<< " Enter Y or N followed by Enter: " << endl;

cin >> x;

if ( x == 'Y')

{

DisplayMenu();

flag = 0;

}

else

{

cout << "Thank you for your time! :)" << endl;

exit(0);

flag = 1;

}

return (flag);

}




int main()

{

Shapes ob;

ob.DisplayMenu();

return 0;

}