Wrong order when executing?

This is a discussion on Wrong order when executing? within the C++ Programming forums, part of the General Programming Boards category; When I make a code like this: Code: cout<<"hello world"; clrscr(); It dóés do the clrscr() command, but before the ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Wrong order when executing?

    When I make a code like this:

    Code:
    cout<<"hello world";
    clrscr();
    It dóés do the clrscr() command, but before the cout! So it first cleans the screen, and thán cout the text! I have the same problem with printing something to the screen and then use getch to continue. In that case it doesn't display the text 'press a key to continue' but it first waits for input and thén puts out the text I use VC++6, do you know how I can make sure the order is correct?

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Strange. So you have a program like

    Code:
    int main (void)
    {
        cout << "hello world";
        clrscr ();
        return 0;
    }
    and it executes clrscr() before cout? What happens on your system when executing something like this:

    Code:
    int main (void)
    {
        cout << "Before clearscreen" << endl;
        clrscr ();
        cout << "After clearscreen" << endl;
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    Hmm... don't know which header file I included last time I tried clrscr(), not conio.h, that one doesn't work. But yes, really strange hey! I'll look up a better example...

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Maybe the compiler is changing the code on you because it thinks that you are messing up. Now that I say this, it sounds pretty dumb but oh well.

  5. #5
    Señor Member
    Join Date
    Jan 2002
    Posts
    561
    This is an easy one. It prints out what you want it to print out, and before you see what it says, the screen is cleared. Try this:
    Code:
    #include<iostream.h>
    #include<conio.h> //change to conio.c if you're using Dev
    
    int main (void)
    {
        cout << "hello world";
        getch();  //pauses until you press somthing
        clrscr ();
        return 0;
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    cout<<"hello world";
    clrscr();

    You're using two different output mechanisms, which are NOT synchronised with each other - cout in particular buffers things until it runs out of buffer space, sees an endl, or is told to flush the buffer.

    So,
    cout<<"hello world" << endl;
    clrscr();

    Or perhaps,
    cout<<"hello world";
    cout.flush();
    clrscr();

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    No no no!!! It's not that the screen is cleared so I can't see the text! The problem is that it first clears the screen and thán displays the text, which should be reversed. I'll try Salem's tip..

    [edit]He thanx, Salem! Adding endl to the end solved the problem [/edit]
    Last edited by R@m0n; 02-10-2002 at 05:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-14-2008, 02:52 PM
  2. FFT returning values in wrong order
    By DavidP in forum C# Programming
    Replies: 3
    Last Post: 10-25-2007, 01:15 PM
  3. Problem with ascending order Program
    By cashmerelc in forum C Programming
    Replies: 4
    Last Post: 09-21-2007, 05:36 PM
  4. Help me sort the names in alpha order.
    By cazil in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2002, 01:30 PM
  5. Replies: 1
    Last Post: 01-06-2002, 05:28 PM

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