-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Sunday 12 July 2015

Delaying the output in C++

Yesterday someone asked me about the function of delay in C++.I was unaware of it.So,I searched its function in C++ and finally I got the answer in a forum.

I decided to share that function with you guys.The reference of that forum will be given at the end of source code.

Delay function

First of all remember that if you want to display the output in slow motion,then you will prefer it.For example,

4 (1st second)
3 (2nd second)
2 (3rd second)
1 (4th second

Below is the source code of this program.Run this code on your compiler and clear your concepts of delay function.

Note: Copying of any content is prohibited on my blog due to security reasons.So,you cannot copy this source code.Sorry for the inconvenience.

Source code


#include <windows.h>
#include<string.h>
#include <iostream>
using namespace std;
int main( ){
    cout << "5" << endl;
    //  We use 1000 since Sleep takes the amount of time in milliseconds, and 1000 ms = 1 second
    Sleep( 1000 );

    cout << "4" << endl;
    Sleep( 1000 );
    cout << "3" << endl;
    Sleep( 1000 );
    cout << "2" << endl;
    Sleep( 1000 );
    cout << "1" << endl;
    Sleep( 1000 );

    cout << "Congratulations, you've just wasted 5 seconds of your life!";
    cin.get( );
    
    return 0;
}

This source code was written in forum by NGen on 31-08-2009.Reference of the forum is given below.


Output

5
4
3
2
1
Congratulations, you've just wasted 5 seconds of your life!

Use of  "Windows.h" header file

windows.h is a Windows-specific header file for the C/C++ programming language which contains declarations for all of the functions in the Windows API, all the common macros used by Windows programmers, and all the data types used by the various functions and subsystems. It defines a very large number of Windows specific functions that can be used in C. The Win32 API can be added to a C programming project by including the <windows.h> header file and linking to the appropriate libraries.

For reference,click the link below.

No comments:

Post a Comment