[Solved] How C++ understand both mode of input? [closed]


The input is what we call stream based. It is not looking at lines of text at all. When it looks for an int, it looks for digit characters, until it finds something that isn’t a digit.

In the first example, when the input is

1
2
3

after it reads each number, it finds a newline character \n, which is not a digit, so it signals the end of the number that was just read, and it works just fine.

In the second example, when the input is

1 2 3

after it reads each number, it finds a space character, which is not a digit, so it signals the end of the number that was just read, and it works just fine.

This is a lot like the way the C++ compiler reads and parses C++ programs. For example, your program would work the same if you jammed almost all of it all onto one line:

#include <iostream>
using namespace std; int main() { int j; for (int i=0; i<3; i++) { cin >> j; cout << j << endl; } }

(But preprocessor directives like #include are different; those must be alone on their own line.)

solved How C++ understand both mode of input? [closed]