开发者

C++ For loop loops only 299 times

I'm having this weird problem. My code is simple:

#include <iostream>
using namespace std;

int main() {
    int num;
    c开发者_开发百科out << "number: ";
    cin >> num;

    for (int i=0;num>i;i++) {
        cout << i <<"\n";    
    }

system ("Pause");
return 0;    
}

If the input for example is 1000, the output contains numbers from 701-999. Any idea?

I'm using Dev-C++ IDE on Parallels.


Actually it prints all of them, from 0 to 999, but your console's buffer is not large enough. So you see only the last part. if you print into a file, not the console, you'll see :)


The loop ends when num>i is no longer true. This occurs when i is 1000, so the last loop executed will be with value 999. As for not seeing lower than 701, maybe your screen buffer is too small.


It will start with 0-999. Also, it appears to you that it starts with 701 because of your console screen settings. If you want to see it for yourself, change the newline into a space:

cout << i <<" "; 


Did 0-700 scroll off the screen? Run your exe like this

your_program > out.txt

Then look at out.txt in an editor.


Works absolutely fine for me. I'd suggest your IDE might be playing tricks on you. Could you redirect output into a file and check that?


Regarding @JoshD answer, You will need to:

 for (int i=0;num>=i;i++) {
    cout << i <<"\n";    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜