Posts Tagged MinGW

A simple source code template that works with MS Visual Studio, MinGW, QT Creator, and NetBeans

I was recently asked by someone starting to learn C++,

“How do you stop the window from disappearing when the when your program ends?”

I know that there’s more than one way to solve this problem, but this is the template I use, I hope it  helps….

/*
    File     :  program_name.cpp
    Author   :  Author Information    
    Date     :  File Creation date    
    Rev Date :  Revision date    
    Platform :  Hardware/Software the code was written and tested on.
    Purpose  :  Short description of what the code from this file does.     
*/
/*
    Header file listing (tells the compiler what support code will be needed)
    Standard headers first < stdio/iomanip/iostream > 
        - some may not need the -.h extension
    Library or SDK headers next < OpenGL/DirectX/SDL/FMOD>
    Custom files next "generic_template.h"  (use quotes NOT brackets)   */ 

//NOTE: MinGW & Microsoft compilers list C++ core header files - .h
#include <iostream>	
// used to call functions to delay window termination
#include <limits>     	

// Replaces std::cout with cout, A namespace is required 
using namespace std; 	

//- Globals (sparingly!)
//- Function Prototypes 

// Listing main with out stating a type is an ISO error
int main() 		
{
/* 	Define and initialize variables at the earliest point in your code
    in for loops (always initialize the counter with its type)
    
        for ( int i = 0; i <= condition; i++ )
    
       Your code should be well documented,  all comments are 
    skipped over by the compiler, the more readable it is the easier 
    to understand the process used.
        
       In some compilers, if the code between this point and the 
    final return statement is left out, during a console window session,
    the window will disappear IMEDIATELY, once the last character 
    is printed.  If info was sent to the console window, to receive user 
    input, and the last thing that was logged was the user pressing 
    the ENTER key, the window will still close rather quickly, in that 
    case add the cin.ignore line a second time, and the console window
    is forced to remain until the user intentionally 
    presses the ENTER key a second time.                                     
*/
    
    //Add this if the console window keeps vanishing
    cout << "\n\nPress ENTER KEY to continue...\n\n";
    
    // Waits for the user to respond, then exits
    cin.ignore(numeric_limits<streamsize>::max(), '\n');         
    
    //Indicates that the program ended successfully
    return 0;
}

, , , , , , ,

Leave a comment