Imagine I have this code:
for (int i=0; i<1000; i ) {
cout << i << endl;
}
So in the console, we see the result is printed once until 999. We can not see the first numbers anymore (let say from 0 to 10 or 20), even we scroll up.
How can I print output page by page? I mean, for example if there are 40 lines per page, then in the console we see 0->39, and when we press Enter, the next 40 numbers will be shown (40->89), and so on, until the last number.
CodePudding user response:
While the previously answers are technically correct, you can only break output at a fixed number of lines, i.e. at least with standard C library only, you have no way to know how many lines you can print before filling the entire screen. You will need to resort to specific OS APIs to know that.
UNIX people may tell you that, in fact, you shouldn't bother about that in your own program. If you want to paginate, you should just pipe the output to commands such as less
or more
, or redirect it to a file and then look at the file with a text editor.
CodePudding user response:
You could use the %
(remainder) operator:
for (int i=0; i<1000; i ) {
std::cout << i << '\n';
if(i % 40 == 39) {
std::cout << "Press return>";
std::getchar();
}
}
This will print lines 0-39, then 40-79 etc.