简体   繁体   中英

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.

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:

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

Note: (cin>>ws) is to get rid of leading whitespaces. Also, c is of type char .

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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