请高手指点一下,源码哪里有问题?
本源码来自《21天学通C++》(Jesse Liberty著)第一篇学习总结。目的是通过一个显示菜单进行功能操作,可以画出矩形、计算面积、周长以及重新设定矩形的长、宽,最后,可以退出菜单和程序。
问题是:哪里出了错? 在输入字符型选择后,程序不停止……
#include<iostream.h>
enum CHOICE{DrawRect=1,GetArea,GetPerim,ChangeDimensions,Quit};//枚举型常量
class Rectangle
{
public:
Rectangle(int width,int height);
~Rectangle();
int GetHeight()const{return itsHeight;}
int GetWidth()const{return itsWidth;}
int GetArea()const{return itsHeight*itsWidth;}
int GetPerim()const{return 2*itsHeight+2*itsWidth;}
void SetSize(int newWidth,int newHeight);
private:
int itsWidth;
int itsHeight;
};
void Rectangle::SetSize(int newWidth,int newHeight)
{
itsWidth=newWidth;
itsHeight=newHeight;
}
Rectangle::Rectangle(int width,int height)
{
itsWidth=width;
itsHeight=height;
}
Rectangle::~Rectangle(){}
int DoMenu();
void DoDrawRect(Rectangle);
void DoGetArea(Rectangle);
void DoGetPerim(Rectangle);
int main()
{
Rectangle theRect(30,5);
int choice=DrawRect;
int fQuit=false;
while(!fQuit) //死循环
{
choice=DoMenu();
if((choice<DrawRect)||(choice>Quit))
{
cout<<"请重新选择\n";
continue;
}
switch(choice)
{
case DrawRect:
DoDrawRect(theRect);
break;
case GetArea:
DoGetArea(theRect);
break;
case GetPerim:
DoGetPerim(theRect);
break;
case ChangeDimensions:
int newLength,newWidth;
cout<<"\nNew width:";
cin>>newWidth;
cout<<"New height:";
cin>>newLength;
theRect.SetSize(newWidth,newLength);
DoDrawRect(theRect);
DoGetArea(theRect);
DoGetPerim(theRect);
break;
case Quit:
fQuit=true;
cout<<"\n退出……\n\n";
break;
default : //可能永远不会出现。但在本程序中,输入字母,出现循环不停
cout<<"选择错误\n";
fQuit=true;
break;
}
}
return 0;
}
int DoMenu()
{
int choice;
cout<<"\n\n****Menu****\n\n";
cout<<"(1)画出矩形。\n";
cout<<"(2)算出面积.\n";
cout<<"(3)计算周长.\n";
cout<<"(4)重新设置矩形。\n";
cout<<"(5)退出。\n\n";
cin>>choice;
return choice;
}
void DoDrawRect(Rectangle theRect)
{
int height=theRect.GetHeight();
int width=theRect.GetWidth();
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
cout<<"*";
}
cout<<"\n";
}
}
void DoGetArea(Rectangle theRect)
{
cout<<"Area:"<<theRect.GetArea()<<endl;
}
void DoGetPerim(Rectangle theRect)
{
cout<<"周长是:"<<theRect.GetPerim()<<endl;
}