problems with numbers and loops

Hello, I've been using loops for a while, mostly small loops, but today i decided to try to make something a little bit bigger, but there was some problem, and i cant figure out what it is.. anyways, heres the code, i've cut it quite a bit to make it shorter and highlight the problem:
1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
    for(int a=0;a<=1000;a++)
    {
            std::cout<<a<<endl;
    }
}

This would, as far as i know, start at 0 and then go up all the way up to a 1000, i can conferm that this works when the number is lower; about 300 will work, but when its 1000, it starts counting up from 702, what gives?

Using dev-c++ and the default compiler, whichever that is.

Any help is appriciated,
Yours,
Chabo
There is nothing there that would cause that problem. The only possible issue would be using an int if you were on an 8 bit architecture but the symptoms would be different.

Try pasting more of the code here, you may be changing 'a' by mistake. By the way if you're prepending cout with std:: you should be doing it with endl as well.
I've compiled that code, with the only exeption beeing that i used using namespace std;

instead of the std:: before the cout, and i cant find the problem nither, but if you can compile the code and go all the way to a 1000, then i guess it must be my compiler, any idea what sort of problem it could be? since the compiler is not returning any error text, could it be that the cmd prompt only displays 300 lines?

EDIT; The problem was indeed that the CMD prompt only displayed 299 lines at one time; when it goes over that, the oldest line gets pushed off
Last edited on
That sounds like it may well be your problem.
Try changing cout<<a<<endl; to
1
2
cout << a << "\t";         //Tab after each number
if (a%5==0) cout << endl;  //Newline after each 5 

This should write 5 numbers per line, so a 300 line buffer would be good for 1500 numbers.
Thanks a lot Faldrax, really great solution,
and while we are on the topic, is there a better program for running your programs then the CMD?

Its almost only text based programs and calculations that im writing atm, and so, i dont really create my own windows,
Glad to help.
When I was initialy learning I would write everything to CMD as it's fairly simple and you can concentrate on how C++ works as a language without worying about the UI too much.
It also has the advantage that code is easy to post to the forum, and will be common across a wide range of OS platforms - so you have a much better chance of finding an example or help that works:-)
Topic archived. No new replies allowed.