Functions and pausing

This is a discussion on Functions and pausing within the C++ Programming forums, part of the General Programming Boards category; Hi I just finished another section on my C++ book which went over functions. I understood it completely and all--or ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    4

    Functions and pausing

    Hi

    I just finished another section on my C++ book which went over functions. I understood it completely and all--or inleast I thought I did. To practice what I just learned I wrote a simple program with two functions, myMessage() and of course main(). main() passes control to myMessage() which in turn displays a message and passes control back to main(). Now, the problem I came across was in pausing the program so I can view the results. No matter where I put the system("pause") or even getch(); functions the program would pause at the beginning.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    // define function that will display message
    void myMessage()
    {
    	cout << "Message from myMessage() here";
    	return;
    }
    
    void main()
    {
    	cout << "Message from main() here\n\n";
    	
    	myMessage();
    	
    	system("pause");
    	return;
    }
    I know there must be some simple mistake as I've been making a lot of those lately. Can someone please enlighten me of a way around this problem?

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Output using cout is buffered. The buffer wont be flushed until after your system("pause") or getch(). You'll either have to manually flush it using cout.flush() or tag an endl or flush onto the end of your output statements -

    cout << "Message from myMessage() here" << flush;

    or get some buffered input (using cin), which will automatically flush the output buffer.
    zen

  3. #3
    Unregistered
    Guest
    Just out of curiousity, why are you including return statements when they are not needed (your functions are void).

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    4
    zen, thank you very much for your reply, flushing the output manually worked very well. and i thought about using cin >> but it didn't seem like good programming etiquette.

    Originally posted by Unregistered
    Just out of curiousity, why are you including return statements when they are not needed (your functions are void).
    the code that i listed was not my original code. it was a little more complicated than this one as I took out some code. i added the return statements just to avoid any confusion since a lot of people are used to seeing them in every function.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a thought: NEVER use 'void main'. Search the board for reasons why. Or just read the FAQ.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pausing Unicode
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 07-16-2002, 03:05 AM
  2. pausing functions..
    By bluehead in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2002, 03:27 AM
  3. pausing the system
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2001, 01:34 PM

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