c++ doesn't require formatting, but I can add indentation if needed.
Clear, logical code layout will, primarily, help you. It will make it easier for you to see at a glance the logic of your code, the flow of control, and the scope of your variables. You'll do yourself a big favour if you get into the habit of using it.
/* 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__
#include <iostream>
#include <limits>
void _pause();
class ProgramPauser
{
public:
ProgramPauser() { }
~ProgramPauser() { _Pause(); }
public:
inlinevoid _Pause() const;
};
using PP = ProgramPauser;
inlinevoid _pause()
{
std::cout << "\nPress ENTER to continue...";
// clear any previous errors in the input stream
std::cin.clear();
// synchronize the input buffer stream
std::cin.sync();
// extract and discard the max number of characters
// until an endline character is reached
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
inlinevoid ProgramPauser::_Pause() const
{
_pause();
}
#endif
C++ only, this is a bit hackish, especially with the _pause()/_Pause() functions having the under-score prefix.
Simply include this stand-alone header in your source, and then create an instance of the class either as a global or in main(). When the object goes out of scope at program termination the destructor does the work.
Or call the non-class function _pause() when needed.
This header was never meant to be released, it was created as an experiment. A "what if."
I don't use it in any code, the idea of keeping the console window is something that only a newbie is concerned about, especially one learning on Windows.
Don't overthink it. There are online code formatters, notepad++ can format, visual studio and other IDE can format, and for the most part, the default formatting done by these tools is more than sufficient to get a good enough layout. If the code is super 'special' you may need to hand-format sometimes, but that is an exercise in inconsistency for most humans and wastes a ton of time. Find a tool that gives output you find to be pretty good, and use that.
@Manga yes! A simple solution. It's been such a pain in the butt just to get a simple io program working smoothly. Now I would like to replace the exit input with a keystroke. Any libraries for keyboard input?
#include <iostream>
#include <conio.h>
usingnamespace std;
int main() {
//start loop
while (true) {
cout << "Ready to quit? Then enter 'y' or 'Y'. ";
char c = 'n';
if (_kbhit()) //if a key pressed
{
cout << "A key was pressed.\n";
c = _getch(); //get that key
}
if (c == 'y' || c == 'Y') { //ends the loop
break;
}
}
return 0;
}