标题:为什么会进入死循环,怎样修改才正确
只看楼主
墨香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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-350409-1-1.html




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

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