follow up: console closing down, code example

Pages: 12
Jul 23, 2018 at 5:26pm
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.
Last edited on Jul 23, 2018 at 5:27pm
Jul 23, 2018 at 5:29pm
@Manga: So I should start by:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 

bool isrunning=True; 
bool ready_to_exit=False
while(isrunning==True)
{
int main()

{ //my program
 
// some input
ready_to_exit=True;
return 0;}

if(ready_to_exit)
{ break;}

}


If this is the solution, how do I post to the other thread? I was no longer able to.
Last edited on Jul 23, 2018 at 5:30pm
Jul 23, 2018 at 5:33pm
@mikeyBoy: what sort of formatting do you prefer? do I indent inside a scope, during definitions, ?
Jul 23, 2018 at 5:45pm
closed account (E0p9LyTq)
Something I put together after reading the "console closing down" sticky. As experiment to see if I could "classify" some of the ideas:

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
34
35
36
37
38
39
40
41
42
43
44
45
/* 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:
   inline void _Pause() const;
};

using PP = ProgramPauser;

inline void _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');
}

inline void 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.
Last edited on Jul 23, 2018 at 5:50pm
Jul 23, 2018 at 5:58pm
closed account (E0p9LyTq)
Willogical wrote:
what sort of formatting do you prefer?

It is more a question of what type of formatting YOU prefer.

The exact style is something you are comfortable with, leading to easily understood and maintain code.

http://mrbool.com/importance-of-code-indentation/29079

https://code.tutsplus.com/tutorials/top-15-best-practices-for-writing-super-readable-code--net-8118
Last edited on Jul 23, 2018 at 6:23pm
Jul 23, 2018 at 6:06pm
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.

Jul 23, 2018 at 7:16pm
A complete example of a working loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main() {
	//start loop
	while (true) {
		cout << "Ready to quit? Then enter 'y' or 'Y'. ";
		char c;
		cin >> c;

		if (c == 'y' || c == 'Y') { //ends the loop
			break;
		}
	}


	return 0;
}
Jul 23, 2018 at 8:24pm
@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?
Jul 24, 2018 at 4:42pm
This looks like crap because it is looping through so fast. But press a 'y' and it closes the loop.

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
#include <iostream>
#include <conio.h>

using namespace 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;
}
Last edited on Jul 24, 2018 at 4:43pm
Topic archived. No new replies allowed.
Pages: 12