views:

2649

answers:

8

One of the first things I learned in C++ was that

#include<iostream>
int main()
{
    std::cout<<"Hello, World!\n";
    return 0;
}

would simply appear and disappear extremely quickly without pause. To prevent this, I had to go to notepad, and save

helloworld.exe
pause

ase

helloworld.bat

This go tedious when I needed to create a bunch of small test programs, and eventually I simply put while(true); at the end on most of my test programs, just so I could see the results. Is there a better wait function I can use?

A: 

You can use sleep() or usleep().

// Wait 5 seconds
sleep( 5 );
jthompson
Note that sleep() isn't part of the C standard. It's defined by Posix, and I believe you have to use Sleep() on Win32.
Bastien Léonard
+13  A: 

you can require the user to hit enter before closing the program... something like this works.

#include <iostream>
int main()
{
  std::cout << "Hello, World\n";
  std::cin.ignore();
  return 0;
}

The cin reads in user input, and the .ignore() function of cin tells the program to just ignore the input. The program will continue once the user hits enter.

Link

DeadHead
+5  A: 

the equivalent to the batch program would be

#include<iostream>
int main()
{
    std::cout<<"Hello, World!\n";
    std::cin.get();
    return 0;
}

The additional line does exactly what PAUSE does, waits for a single character input

TokenMacGuy
+1  A: 

Syntax:

void sleep(unsigned seconds);

sleep() suspends execution for an interval (seconds). With a call to sleep, the current program is suspended from execution for the number of seconds specified by the argument seconds. The interval is accurate only to the nearest hundredth of a second or to the accuracy of the operating system clock, whichever is less accurate.

This example should make it clear:

#include <dos.h>
#include <stdio.h>
#include <conio.h>

int main()
{
   printf("Message 1\n");
   sleep(2); //Parameter in sleep is in seconds
   printf("Message 2 a two seconds after Message 1");
   return 0;
}

Remember you have to #include dos.h

EDIT:

You could also use winAPI.

VOID WINAPI Sleep(
DWORD dwMilliseconds
);

Sleep Function(Windows)

Just a note,the parameter in the function provided by winapi is in milliseconds ,so the sleep line at the code snippet above would look like this "Sleep(2000);"

John
A: 

getchar() provides a simplistic answer (waits for keyboard input). Call Sleep(milliseconds) to sleep before exit. Sleep function (MSDN)

Eric Bainville
A: 

The second thing to learn (one would argue that this should be the first) is the command line interface of your OS and compiler/linker flags and switches.

Nikolai N Fetissov
+3  A: 

The appearance and disappearance of a window for displaying text is a feature of how you are running the program, not of C++.

Run in a persistent command line environment, or include windowing support in your program, or use sleep or wait on input as shown in other answers.

dmckee
+1  A: 

What you have can be written easier. Instead of:

#include<iostream>
int main()
{
    std::cout<<"Hello, World!\n";
    return 0;
}

write

#include<iostream>
int main()
{
    std::cout<<"Hello, World!\n";
    system("PAUSE");
    return 0;
}

The system function executes anything you give it as if it was written in the command prompt. It suspends execution of your program while the command is executing so you can do anything with it, you can even compile programs from your cpp program.

poliklosio