Pause function ???

This is a discussion on Pause function ??? within the C++ Programming forums, part of the General Programming Boards category; I'm looking for a pause function to use at the end of a simple C++ program to pause the screen ...

  1. #1
    Unregistered
    Guest

    Pause function ???

    I'm looking for a pause function to use at the end of a simple C++ program to pause the screen so the user can see the output before it closes. I know there is a simple function that will do this, but I can't remember/find it anymore. Anyone who knows a function that will pause the screen or wait till a key is pressed? Thanks!

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    Code:
    // Pause for the specfied time
    
    #include <time.h>
    
    void pause(int secs)
    {
        long tm1,test;
        time(&tm1);
        test = tm1 + secs;
        while(tm1 < test) 
        {
             time(&tm1);
        }
    }
    
    // pause till any key is hit
    #include <conio.h>
    #include <stdio.h>
    
    void pause(void)
    {
        while(!_kbhit());
        getch(); // clear the key from the stdin
    }
    
    
    // Wait untill a specific key is hit
    #include <conio.h>
    #include <stdio.h>
    
    void pause(char key)
    {
        char ch;
        begin:
        while(!_kbhit());
        ch = getch();
        if(ch == key) { return; }
        else
        goto begin;
    }

  3. #3
    Unregistered
    Guest

    Clear buffer ???

    How do I clear the buffer before I use this method of pausing? My output is not appearing until after I hit a key.

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    whats the line of code that gets the input?

  5. #5
    Unregistered
    Guest

    code

    I am using cin and cout to display some stuff... and then I call a pause function. Similiar to....

    cin >> Name
    cout << "Hello ";
    cout << Name << "\n";
    Pause()

    Pause function is....

    void Pause()
    {
    char keyHit;

    cout << "Press Any Key to Continue\n";

    // Pause until the user presses a key.
    getch();
    return;
    }

    It does pause the screen waiting for any character to be pressed, but the output...

    Hello Sam

    is not shown until the additional character is pressed.

    I assume that this is because the buffer is not getting sent to the screen before hand. Maybe I'm wrong. ???

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    i can't remember off-hand what causes this but this should fix it if not let me know and we'll try something else.
    try

    cout.flush();

    before you call pause() this should fix it.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    when using cout you must use endl
    like this:
    cout << "Enter a number: " << endl;

    endl flushes the output to the screen and forces the cursor to the next line. To avoid forcing it to the next line use flush instead
    like this:
    cout << "Enter a number: " << flush;

    and to pause the screen long enough to see the screen use the getchar(); //requires stdin.h I believe waits for keyboard entry

    Also you can run your program from the command line by first opening MS DOS and changing the current directory to the location of your executable or placing it the c:\windows\command\ folder or .... change the path to some folder where you keep your executables..
    zMan

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    With Vis C++ can't you just

    Code:
    #include<windows.h>
    
    //code here
    
    Sleep(x)
    Before you criticise a man, walk a mile in his shoes. Then, when you criticise him, you're a mile away. And you have his shoes.

  9. #9
    Unregistered
    Guest
    Why not just use "system("pause");" Then when <enter> is pressed it goes away

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

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