Xralier Xralier - 1 month ago 13
C++ Question

Why this code is printing 0 to 297 but not 0 to anything greater than 297?

As you can see, in this code , im just trying to print 0 to m . ive used exactly "m" as a variable.(ignone "n", the main thing is "m" here) if i input m = 5 ( accually i will type 4 to input "n" in my keyboard but ignore this , the "m" will be 5 ) .
so the code will print 0 1 2 3 4 5 . The problem i face is i can see 0 to m numbers perfectly only when m is 297 or less then 297. when i input m more than 297 , for example 500 . i can see numbers from 203 - 500 only . why i am not seeing 0 - 500?

#include <bits/stdc++.h>
using namespace std;
int main() {
long n, m;


cin >> n;
m = n + 1;

vector<long> v(0);

for (long i = 0; i < m; ++i) {
v.push_back(i);
}

for (long j = 0; j < m; ++j) {
cout << (v.at(j)) << endl;
}
return 0;
}

Answer

This prints [0, 500] fine for me so it's probably a scrollback limitation. Check the settings on your terminal if you're using one.

If you're on a unix-like system you can use the wc command to count the number of lines printed and confirm this for yourself:

$ echo "500" | ./a.out | wc -l
501

You can also send the output to less which will let you scroll through it

$ echo "500" | ./a.out | less