Thread: Delayed Functions?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    18

    Question Delayed Functions?

    Code:
    #include <stdlib.h>
    #include <time.h>
    #include <iostream.h>
    #include <string.h>
    #include "Console.h"
    
    namespace con = JadedHoboConsole;
    
    inline void drawline(int length)
    {
    	int i;
    	for (i=0; i<length; i++)
    	{
    		cout << (char)205;
    	}
    }
    
    // Main Menu
    void menu()
    {
    	// Clear Screen
    	using std::cout;
    	using std::endl;
    	cout << con::clr;
    
    	cout << (char)201;
    	drawline(20);
    	cout << (char)203 << endl;
    	cout << (char)186 << "(L)2005" << char(186) << "Blah Blah Blah" << endl;
    	cout << (char)204;
    
    }
    
    int main()
    {
    	menu();
    	return 0;
    }
    This code compiles fine (no warnings) but when executed, the function drawline is not called until after all the other couts. Any idea on what the problem is?

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You probably need an endl in the function. I've had similar out-of-sequence problems when I used '\n' in place of 'endl'. endl flushes the buffer. \n does not. Try putting this line: cout << (char)203 << endl; into your drawline() function.

    I don't know why this happens... I'm sure your program is executing in the correct sequence. It's just that the output is not gettting displayed in the correct sequence. I believe this is an operating system issue... maybe Windows has multiple stdout buffers???
    Last edited by DougDbug; 03-07-2005 at 04:42 PM.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If it's out of sequence them something if very wrong (and unlikely).

    The output probably just hasn't been flushed to the screen as DougDbug suggested. You can also flush the stream without adding a newline like so: "cout << flush;".

    gg

  4. #4
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    I have the same problem with MessageBox();

    Code:
    cout << "I love you......";
    cout << "a lot.";
    MessageBox(0,"I love you.","Guess what?",0);
    The message box will pop up before the cout messages are displayed.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    18
    whoa. flush; worked like a charm. thanks a bunch guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM