简体   繁体   English

如何编写一个带有两个整数的while循环并以'|'结束 在c ++中?

[英]How to write a while loop that takes two ints and terminates with '|' in c++?

I'm working through a text book for self study. 我正在编写一本自学书。 I can do the while loop no problem, but I have no idea how to do the terminating character. 我可以做while循环没问题,但我不知道如何做终止字符。

Here is what I have now: 这就是我现在拥有的:

#include "../../std_lib_facilities.h" // Supplied by book author

int main()
{
    int ii = 0;
    int yy = 0;
    bool test = true;

    cout << "Enter two ints" << endl;

    while (test)
    {
        cin>>ii, cin>>yy;

        // this if statement doesn't work

        if (ii == '|' || yy == '|')
        { 
            test = false;
        }

        // this if statement catches all bad input, even the terminating '|'

        if (cin.fail())
        {  
            cout << "bad input";
            cin.clear();
            cin.ignore();
            continue;
        }
        else 
            cout << ii << yy << endl;

    }

    return 0;
}

Streams can be a little confusing if you're unfamiliar with them. 如果您不熟悉它们,流可能会有点混乱。 It's a large topic that's just going to require more research. 这是一个很大的话题,需要更多的研究。 Here's an example that should work, to hopefully get you started. 这是一个应该有用的例子,希望能让你开始。

int main(int argc, char* argv[])
{
    bool test = true;
    while ( test ) {
        std::cout << "Enter two integers> ";

        int x, y;
        // if this fails, stream is bad.
        // @note this will fail for any input which cannot be interpreted
        // as an integer value.
        if (std::cin >> x >> y) {
            std::cout << x << " " << y << std::endl;
        }
        else {
            // clear stream error state so we can read from it again.
            std::cin.clear();
            // check for terminating character; else unknown.
            if (std::cin.get() == '|')
                std::cout << "Terminator found, exiting." << std::endl;
            else
                std::cerr << "Error: bad input, exiting." << std::endl;
            // in either case the loop terminates.
            test = false;
        }
    }
    return 0;
}

Hope this helps. 希望这可以帮助。 Good luck. 祝好运。

Use the cin.peek() function as follows, before you input the two numbers: 在输入两个数字之前,请使用cin.peek()函数,如下所示:

 c=(cin >> ws).peek();
    if(c=='|')
    {
        cout<<"exiting";return 1;
    }

Note: (cin>>ws) is to get rid of leading whitespaces. 注意: (cin>>ws)是摆脱领先的空格。 Also, c is of type char . 此外, cchar类型。

The complete code now looks like this: 完整的代码现在看起来像这样:

int main()
{
    int ii = 0;
    int yy = 0;
    bool test = true;

    cout << "Enter two ints" << endl;

    while (test)
    {
        char c;
        c=(cin >> ws).peek();
        if(c=='|')
        {
            cout<<"exiting";return 1;
        }
        cin>>ii, cin>>yy;

        if (cin.fail())
        {
            cout << "bad input";
            cin.clear();
            cin.ignore();
            continue;
        }
        else
            cout << ii << yy << endl;

    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM