I read this thread "Console Closing Down" http://www.cplusplus.com/forum/beginner/1988/ with interest and thought it might be interesting and personally instructive to boil all the suggestions down into a C++ class, one I call the ProgramPauser class.
Am I overthinking things? Are there easier methods? Is this really as platform independent as it should be?
/* user created functions and classes for flushing the input stream and pausing the program
*
* C++ Header: program_pauser.hpp */
#ifndef __PROGRAM_PAUSER_HPP__
#define __PROGRAM_PAUSER_HPP__
void _pause();
class ProgramPauser
{
public:
ProgramPauser();
~ProgramPauser();
public:
void _Pause() const;
};
using PP = ProgramPauser;
#endif
/* user created functions and classes for flushing the input stream and pausing the program
*
* C++ Source: program_pauser.cpp */
#include <iostream>
#include <limits>
#include "program_pauser.hpp"
void _pause()
{
std::cout << "\nPress ENTER to continue...";
std::cin.clear();
std::cin.sync();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
ProgramPauser::ProgramPauser()
{
}
ProgramPauser::~ProgramPauser()
{
_pause();
}
void ProgramPauser::_Pause() const
{
_pause();
}
/* user created functions for stripping input and pausing the program
*
* C Header: program_pauser.h */
#ifndef __PROGRAM_PAUSER_H__
#define __PROGRAM_PAUSER_H__
/* strips the input queue of unwanted characters */
void _flush_input();
/* pauses the program, strips the input queue of unwanted characters */
void _pause();
#endif
/* user created functions for stripping input and pausing the program
*
* C Source: program_pauser.c */
#include <stdio.h>
#include "program_pauser.h"
char ignore_array[256] = "";
void _flush_input()
{
fflush(stdin);
}
void _pause()
{
printf("\nPress ENTER to continue...");
fflush(stdin);
fgets(ignore_array, 256, stdin);
}
Applications written for a console platform, should be executed from within a console shell. Whether the console remains open after execution or not should be an end user preference. That is if they want the data that was sent to cout, they will launch your application from within a persistent shell. This kind of thing absolutely should NOT be hardcoded into an application.
Great theory, but it doesn't really answer my question(s). Is this good coding IF an end-user demands a program open and close a console window, without the requirement of pre-opening a console shell.
Further, neither of those solutions will behave like you expect.
The first because you cannot guarantee synchrony with input.
The second because fflush(stdin) is a non-standard hack.
Unfortunately, the only ways to do it is one of:
1 - always keep your input synchronized to '\n'+1 (in which case the first solution would work fine)
2 - use platform-specific code to clear and synchronize the input.
Notes:
cin.clear() resets error flags, nothing more.
cin.sync(), if it actually does anything, only updates the istream's internal state; it does not sync the external io state.
Alas. I have yet to write a definitive answer for this (other than 'don't do that').
Thank you, Duoas, you have given me some insight to think about.
To be honest this was not meant to be production-level code, just a learning experience. So far I've learned a lot between this and the stickied thread. :)
Learning C++ from books and internet examples, without any formal class-room training, can be difficult.