ITPub博客

首页 > > > 新手问路:in.get()应该放while(){}里边还是外边?

新手问路:in.get()应该放while(){}里边还是外边?

作者:daxuesheng 时间:2019-09-03 11:30:06 0 删除 编辑

新手问路:in.get()应该放while(){}里边还是外边?

代码如下

void A::upwords()
{
    char ch;
    while (!i.eof())//在stack overflow 上说用eof()不好,why?
    {
        i.get(ch);
        if (ch=='I')
        {
            i.get(ch);
            if (ch == 'S')
            {
                a += 1;
                std::cout << a;
                std::cout<< '\t' << ch << std::endl;
            }
        }
    }i.close();
}

和下面这个

void A::upwords()
{
    char ch;
    i.get(ch);
    while (!i.eof())
    {
        if (ch=='I')
        {
            i.get(ch);
            if (ch == 'S')
            {
                a += 1;
                cout << a;
                cout<< '\t' << ch << std::endl;
            }
        }
    i.get(ch);
    }
    i.close();
}

可是之前下面这个为什么运行不了?

顺便还问一个:

为甚吗while()里边用eof()不好?


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4692/viewspace-3145874/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-05-22

  • 博文量
    176
  • 访问量
    24989