Problem with code (newbie question)

This is a discussion on Problem with code (newbie question) within the C++ Programming forums, part of the General Programming Boards category; #include <stdlib.h> #include <iostream.h> int main(void) { int i; for (i = 0; i < 1000; i++) { cout<<"00101001101010110"; i++; ...

  1. #1
    Unregistered
    Guest

    Talking Problem with code (newbie question)

    #include <stdlib.h>
    #include <iostream.h>

    int main(void)
    {
    int i;

    for (i = 0; i < 1000; i++)
    {

    cout<<"00101001101010110";
    i++;

    }
    system("CLS");
    return 0;
    }


    Well when I run this program there is still a little bit of 0101011101100.... and the screen was not totally cleared. :*(

    thanks in advance

  2. #2
    Unregistered
    Guest
    Add the line:
    cout<<endl;
    just before the call to system("cls")

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    i think the program cleared the screen before printing all of the data out. i'm not really sure on this, though.

    one technical warning: you don't need another i++ at the end of the for loop. the one in the declaration should do the job fine. if you want to skip 2 each iteraton, do i+=2;

    //edit: beaten...
    endl flushes the output buffer, so that should do the trick

  4. #4
    Unregistered
    Guest

    Thumbs up

    Thanks Unregistered. Ive been banging my head against the wall tyring to figure this out and the answer is so simple! hehe.

  5. #5
    Unregistered
    Guest
    I posted reply #2 above. I think the technical answer to this is that the cout object ( a global instance of a class ) has an internal buffer that is not immediately flushed to the Windows screen buffer. Your output fills the cout buffer, & is flushed, then the remainder is written to the cout buffer, and is not flushed. Then you clear the screen. Then the program exits, causing the cout object's destructor to fire off, which flushes the remaining portion of the buffer to the screen buffer. Just a guess on all that.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    52
    One really shoudnt use system() calls in the first place, though. Check out the faq.
    Turn no more aside and brood
    Upon love's bitter mysteries
    For Fergus rules the brazen cars...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd problem in main(), code won't proceed
    By aciarlillo in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2005, 12:00 AM
  2. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 03:02 AM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. Newbie question on numbers.
    By thes in forum C++ Programming
    Replies: 14
    Last Post: 06-17-2002, 08:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21