错误的输入为什么导致无限循环?????????????????
程序代码:#include <iostream.h>
#include <stdio.h>
class rectangle
{
public:
rectangle(int width,int height);
~rectangle();
int getheight() {return itsheight;}
int getwidth() {return itswidth;}
int getarea() {return itsheight * itswidth;}
int getperim() {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=1;
int quit =0;
while(!quit)
{
choice=domenu();
cout << choice << endl;
getchar(); //如果输入的是字符和汉字等,用强制转换int输出的结果竟然是0和00等
if(choice < 1 || choice > 5)
{
cout << "\nninvaid choice, please try again、\n\n";
continue; //这里返回while后,就一直重复执行无限循环了,怎么不调用menu函数呢?
}
switch(choice)
{
case 1:
dodrawrect(therect);
break;
case 2:
dogetarea(therect);
break;
case 3:
dogetperim(therect);
break;
case 4:
int newlength,newwidth;
cout << "\nnew width:";
cin >> newwidth;
cout << "new heigth:";
cin >> newlength;
therect.setsize(newwidth,newlength);
dodrawrect(therect);
break;
case 5:
quit=1;
cout << "\n exiting.....\n\n";
break;
default:
cout << "erroe in choice! \n";
quit =1;
break;
}
}
return 0;
}
int domenu()
{
int choice;
cout << "\n\n ***菜单***\n";
cout << "(1)用#画出这个长方形 \n";
cout << "(2)求面积\n";
cout << "(3)求周长\n";
cout << "(4)重新输入长度和宽度\n";
cout << "(5)退出\n";
cin >> choice;
cout << 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 << "宽度是:" << therect.getheight() << "长度是:" << therect.getwidth() << "面积是:" << therect.getarea() << endl;
}
void dogetperim(rectangle therect)
{
cout << "宽度是:" << therect.getheight() << "长度是:" << therect.getwidth() << "周长是:" << therect.getperim() << endl;
}为什么输入的如果不是数字就会无限循环,知道死机!!!!!!!!!!!!!!!!!!!!!









