How do I add a statement so that when i input a number other than 1-5, the program doesn't fly into a tangent and instead gives an error message?
Also, is there anything cool that I can add to this program to make it better?
Code:
#include <iostream.h>
typedef unsigned short int USHORT;
typedef unsigned long int ULONG;
enum BOOL {FALSE, TRUE};
enum CHOICE {DrawRect = 1, GetArea,
GetPerim, ChangeDimensions, Quit};
// Rectangle Class Declaration
class Rectangle
{
public:
//constructors
Rectangle(USHORT width, USHORT Height);
~Rectangle();
//accessors
USHORT GetHeight() const { return itsHeight;}
USHORT GetWidth() const { return itsWidth; }
ULONG GetArea () const { return itsHeight * itsWidth; }
ULONG GetPerim () const { return 2*itsHeight + 2*itsWidth; }
void SetSize (USHORT newWidth, USHORT newHeight);
// Misc. Methods
void DrawShape() const;
private:
USHORT itsWidth;
USHORT itsHeight;
};
//Class method implementations
void Rectangle::SetSize(USHORT newWidth, USHORT newHeight)
{
itsWidth = newWidth;
itsHeight = newHeight;
}
Rectangle::Rectangle(USHORT width, USHORT height)
{
itsWidth = width;
itsHeight = height;
}
Rectangle::~Rectangle() {}
USHORT DoMenu();
void DoDrawRect(Rectangle);
void DoGetArea(Rectangle);
void DoGetPerim(Rectangle);
void main()
{
//initalize a rectangle to 10,20
Rectangle theRect(30,5);
USHORT choice = DrawRect;
USHORT fQuit = FALSE;
while (!fQuit)
{
choice = DoMenu();
if (choice < DrawRect || choice > Quit)
{
cout <<"\nInvalid Choice, please try again.\n\n";
continue;
}
switch (choice)
{
case DrawRect:
DoDrawRect(theRect);
break;
case GetArea:
DoGetArea(theRect);
break;
case GetPerim:
DoGetPerim(theRect);
break;
case ChangeDimensions:
USHORT newLength, newWidth;
cout <<"\nNew width:";
cin >> newWidth;
cout << "New Height: ";
cin >> newLength;
theRect.SetSize(newWidth, newLength);
DoDrawRect(theRect);
break;
case Quit:
fQuit = TRUE;
cout <<"\nExiting...\n\n";
break;
default:
cout <<"Error in choice!\n";
fQuit = TRUE;
break;
} //end switch
} //end while
} //end main
USHORT DoMenu()
{
USHORT choice;
cout <<"\n\n***Menu*** \n";
cout <<"(1) Draw Your Rectangle\n";
cout <<"(2) Area\n";
cout <<"(3) Perimeter\n";
cout <<"(4) Resize\n";
cout <<"(5) Quit\n";
cin >> choice;
return choice;
}
void DoDrawRect(Rectangle theRect)
{
USHORT height = theRect.GetHeight();
USHORT width = theRect.GetWidth();
for (USHORT i = 0; i<height; i++)
{
for (USHORT j = 0; j<width; j++)
cout <<"*";
cout<< "\n";
}
}
void DoGetArea (Rectangle theRect)
{
cout <<"Area: " <<theRect.GetArea() << endl;
}
void DoGetPerim (Rectangle theRect)
{
cout <<"Perimeter: " <<theRect.GetPerim() << endl;
}
:rock: