| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 471 人关注过本帖
标题:错误的输入为什么导致无限循环?????????????????
取消只看楼主 加入收藏
yang0401
Rank: 2
等 级:论坛游民
帖 子:84
专家分:57
注 册:2011-5-23
结帖率:69.23%
收藏
已结贴  问题点数:20 回复次数:1 
错误的输入为什么导致无限循环?????????????????
程序代码:
#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;
}
为什么输入的如果不是数字就会无限循环,知道死机!!!!!!!!!!!!!!!!!!!!!
2012-04-19 10:33
yang0401
Rank: 2
等 级:论坛游民
帖 子:84
专家分:57
注 册:2011-5-23
收藏
得分:0 
怎么解决这种问题呢?防止输入的类型不正确导致程序奔溃?
2012-04-23 13:49
快速回复:错误的输入为什么导致无限循环?????????????????
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.054509 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved