| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 608 人关注过本帖
标题:为什么会进入死循环,怎样修改才正确
只看楼主 加入收藏
墨香555
Rank: 2
等 级:论坛游民
帖 子:115
专家分:31
注 册:2010-10-21
结帖率:96.15%
收藏
已结贴  问题点数:20 回复次数:2 
为什么会进入死循环,怎样修改才正确
int setgolf(golf & g)
{
    int next;
    std::cout << "Please enter golfer's full name: ";
    std::cin.getline(g.fullname, Len);
    if (g.fullname[0] == '\0')
        return 0;                // premature termination
    std::cout << "Please enter handicap for " << g.fullname << ": ";
    while(!(std::cin >> g.handicap))
    {
        std::cin.clear();
        std::cout<<"please enter a integer: ";
    }
    while (std::cin.get() != '\n')
        continue;
    return 1;
}
Enter up to 5 golf team members:
Please enter golfer's full name: reg
Please enter handicap for reg: w(输入w后就进入死循环)
搜索更多相关主题的帖子: golf next continue please return 
2011-09-20 17:05
nicum
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:180
专家分:712
注 册:2011-2-1
收藏
得分:20 
程序代码:
int setgolf(golf & g)
{
    int next;
    std::cout << "Please enter golfer's full name: ";
    std::cin.getline(g.fullname, Len);
    if (g.fullname[0] == '\0')
        return 0;                // premature termination
    std::cout << "Please enter handicap for " << g.fullname << ": ";
    while(!(std::cin >> g.handicap))              //当输入w的时候,cin>>g.handicap返回真,退出while循环
    {
        std::cin.clear();
        std::cout<<"please enter a integer: ";
    }
    while (std::cin.get() != '\n')              //如果w输完按了回车,while循环也立即退出
        continue;
    return 1;
}
Enter up to 5 golf team members:
Please enter golfer's full name: reg
Please enter handicap for reg: w(输入w后就进入死循环)


[ 本帖最后由 nicum 于 2011-9-20 18:39 编辑 ]
2011-09-20 18:27
墨香555
Rank: 2
等 级:论坛游民
帖 子:115
专家分:31
注 册:2010-10-21
收藏
得分:0 
我知道啦 在while循环里加一个std::cin.get(next);就不会进入死循环啦
2011-09-21 14:48
快速回复:为什么会进入死循环,怎样修改才正确
数据加载中...
 
   



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

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