C++ Class to stop Console Close Down

closed account (E0p9LyTq)
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?

program_pauser.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* 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 


program_pauser.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* 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();
}
Last edited on
closed account (E0p9LyTq)
I also created a C version, as follows.

program_pauser.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* 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 


program_pauser.c:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* 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);
}
Last edited on
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.
closed account (E0p9LyTq)
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').
closed account (E0p9LyTq)
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.
Agreed. It's how I learned until I took some 300+ level university courses.
Topic archived. No new replies allowed.