strange code

Discussion in 'C++' started by johanjohan, Aug 28, 2007.

  1. johanjohan

    johanjohan New Member

    Joined:
    Aug 19, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Ive written this code in different ways and it doesnt work right


    if 100 is used in the prgram it works fine. It will count up or down to 100 or from 100 just fine. But when you put 1000 or 1000. For example : if i have the number 1000 in the program, it starts counting at 702 and not at 1.


    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
        
    int number;
        for ( number = 1; number < 1001; number++)
        cout << number << "is this working" <<endl;
    
    system("pause");
    return 0;
    } 
    
    its also been written like this (and doesnt work)
    Code:
    #include<iostream>
    
    int main()
    {
        
    int number;
        for ( number = 1; number < 1001; number++)
        std::cout << number << "is this working" <<std::endl;
    
    system("pause");
    return 0;
    } 
    
    its also been written like this (and doesnt work)
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
        
    
        for ( int number = 1; number < 1001; number++)
        cout << number << "is this working" <<endl;
    
    system("pause");
    return 0;
    } 
    
    it's been written many ways and each time it doesnt work, it always starts of at 702

    The thing I dont understand is why does it pick the number 702 to start counting from?

    again, with 100 it wont do this, it runs properly. With 1000 and 10000, ive tried using dopuble instead of in. no use.

    thank you
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    It works. Your output has merely overflowed the screen buffer because your screen buffer is not large enough. You can only scroll back to the beginning of the screen buffer, which, in your case, is where the 702 line is.
     
  3. johanjohan

    johanjohan New Member

    Joined:
    Aug 19, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    oh ok, thanks

    is there a way I can fix that so that i can go back too 1 or is that a fundemntal thing?

    if I write a program that goes to 10000 and it starts counting at 9702 to 10000, should I consider it working?

    thank you
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You can increase the buffer size, but not indefinitely, of course. You could ask it to print every tenth line, or every 100th or 1000th, or whatever, by using the modulus operator. If you wanted to see them all, you could pause it after each line. I doubt you want to do that.

    The other thing you can do is write the output to a file instead of the screen.
     
  5. johanjohan

    johanjohan New Member

    Joined:
    Aug 19, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice