Why does this code cause an infinity loop when '|' is introduced in C ++?

I read Programming: Principles and Practice Using C ++ (2nd Edition)
I found this question:

  • Write a program consisting of a while loop that (each time around the loop) reads in two ints and then prints them. Exit the program when '|' ends.

Here is what I tried:

#include <iostream>
using namespace std;
int main () {
int x, y;
while (x != '|' || y != '|'){
    cin >> x;
    cin >> y;
    cout << x << endl;
    cout << y << endl;
}

return 0;
}

When '|', it prints something like an endless loop, unexpected exits.

  • What is happening there?
  • What have I done wrong?
+4
source share
3 answers

, x y -, '|' while. , , .


, , int, cin >> something , , .

(, |), cin >> , , .

, , , | , , ad infinitum - : )


, , , , |. , . , , if (stream >> variable).

cin.peek(), , cin.get(), . , peek, get , operator>>.

- :

#include <iostream>
#include <cctype>

int main() {
    int x, y;

    while (true) {
        // Skip all white space to (hopefully) get to number or '|'.

        while (std::isspace(std::cin.peek())) std::cin.get();

        // If it '|', just exit, your input is done.

        if (std::cin.peek() == '|') break;

        // Otherwise, try to get two integers, fail and stop if no good.

        if (! (std::cin >> x >> y)) {
            std::cout << "Invalid input, not two integers\n";
            break;
        }

        // Print the integers and carry on.

        std::cout << "You entered " << x << " and " << y << "\n";
    }

    return 0;
}

, ( ):

pax$ ./myprog </dev/null
Invalid input, not two integers

pax$ echo hello | ./myprog
Invalid input, not two integers

pax$ echo 1 | ./myprog
Invalid input, not two integers

pax$ echo 1 2 | ./myprog
You entered 1 and 2
Invalid input, not two integers

pax$ printf '1 2|' | ./myprog
You entered 1 and 2

pax$ printf '1 2\n3 4\n5     6 7   8   \n|' | ./myprog
You entered 1 and 2
You entered 3 and 4
You entered 5 and 6
You entered 7 and 8

pax$ printf '1 10     11   12   13    14    |   ' | ./myprog
You entered 1 and 10
You entered 11 and 12
You entered 13 and 14
+5

paxdiablo, , int, char.

, int char, cin.peek(), , '|' .

#include <iostream>
using namespace std;
int main()
{
    int x, y;
    while(cin.peek() != '|') //cin.peek() will return the next character to be read
    {
        cin >> x >> y;
        cout << x << ' ' << y << '\n';
        cin.ignore(); //ignore whitespace/linebreak character left by extraction (>> operator)
    }
}
+3

x, y , char.

#include <iostream>
using namespace std;
int main ()
{
    char x=NULL, y=NULL;
    while (x != '|' || y != '|')
    {
        cin >> x;
        cin >> y;
        cout << x << endl;
        cout << y << endl;
    }

    return 0;
}

. . ,

#include <iostream>
using namespace std;
int main ()
{
    string  x="", y="";
    while (x != "|" || y != "|")
    {
        cin >> x;
        cin >> y;
        cout << x << endl;
        cout << y << endl;
    }

    return 0;
}
-1

Source: https://habr.com/ru/post/1694889/


All Articles