Prevent console from exiting

I am trying to do console application, that only exits, when user press 'x' ( close ) button, because I want it get working for all the time, until it closes :D. Any idea how to make it? For now I have 'for ( ; ; )' in main function, is it allright, or there are any better way?
Last edited on
well, u can try use while loop, like

1
2
3
4
5
6
7
8
int main()
{
	// stuff that will execute at start program
	while(1)
	{
		// stuff that will run until 1 will be true (forever), example: if u put cout << "aha" << endl; there, it will cout aha about 1000 times per second
	}
}
but I'm not sure this will help you

edit: this will say "starting" on start, and it won't close until you will enter "x" or close with X

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	cout << "Starting..." << endl;
	char cExit;
	while(1)
	{
		cin >> cExit;
		if(cExit == 'x')
		{
			break;
		}
	}
	return 0;
}
Last edited on
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
#include <Windows.h>
#include <conio.h>
#include <iostream>
using namespace std;
class Pause
{
public:
	void Clear ()
	{
		for (int i = 0 ; i < 100 ; i++)
		{
			cout << "\n" << endl;
		}
	}
	void STOP ()
	{
		while (true)
		{
			cout << "\nSTOP!!!!" << endl;
		}
	}
	void Wait (int Time)
	{
		cout << "\nPlease Wait :)" << endl;
		Time *= 1000;
		Sleep (Time);
	}
	void PressKey ()
	{
		cout << "\nPress any key to continue . . .";
		while (!getch ())
		{
		}
	}
	void WaitandBeep (int Time)
	{
		cout << "\nPlease Wait :)" << endl;
		Time *= 1000;
		Sleep (Time);
		Beep (4000 , 10000);
	}
};

All of this with out using system () :)
themassivechipmunk ? I can't stop laughing, did u wanted to show us how good are u or what? :D he just wanted simple code to keep his application running, not whole class, lul, fail

btw clear function? with 100x "\n" ? are u serious? just wasting of memory, you can use system("cls") instead

stop function - lal? do u know what u did? this will say "stop" like 1000 times per second and it won't stop until u close program :-X

presskey - useless function, we have system("pause") for that
Last edited on
Or if u are on unix system("clear");
@mekkatorqu: Please follow the rules of this forum and don't bash other users for helping. I should mention that if you would like to offer constructive criticism, you should do so with proper grammar and typing.

@Scottas: Many a user has come to this forum to ask this same type of question. Those ideas are all compiled here: http://cplusplus.com/forum/beginner/1988/ If you read enough there, you might notice system calls are not recommended by more seasoned programmers for various reasons. Hope that page helps your programming :)
@mekkatorqu: Please follow the rules of this forum and don't bash other users for helping. I should mention that if you would like to offer constructive criticism, you should do so with proper grammar and typing.


I think that the jab was warrented, the post that TheMassiveChipmunk wrote was useless and stupid, and nowhere near helpful (c'mon, try some of his examples, they suck). Not to mention the fact that he didn't explain a thing (especially why not to use system () functions or what the hell they even are).

Maybe if you want to act all moderator like, check out chipmunks code and chirp him a little bit for the use of such poor ideas, just throwing the code out with no explanation at all, and the use of non-standard headers (conio.h...).
I personally liked the wait and beep... until I tried to compile and run it... and discovered a sad day when it didn't work :( So now I have no reason to even attempt to defend the code at all.
Last edited on
Or you could just sit in your chair and wait one second and go "Beep", then wait another second and go "Beep", repeating this until someone hits "ENTER" (or just hits). At least this way you can make different tones without writing more code.
Oh sorry no. I was not showing off or anything I was just taught that system was a BAD thing so I thought that would be a good lesson to pass on. Sorry :)
Last edited on
Oh sorry no. I was not showing off or anything I was just taught that system was a BAD thing so I thought that would be a good lesson to pass on. Sorry :)
The reason system is bad is because it is only OS specific.


system () functions are still better than what you posted, at least they work.

Edit:

Isn't getch () platform dependant as well? I know it's non-standard, and I think (not 100% sure) that I read somewhere that it is platform dependant too.
Last edited on
I have read that system was a bad thing but I was in the habit of using it. So it was very hard for me to kick the habit. I thought it would be nice. I am sorry if it came out n00bish.
@kevinkjt2000
The Class Worked well for me are you sure you are not doing anything wrong?
:)
http://www.cplusplus.com/forum/beginner/3304/
This is were I read system is bad.
Last edited on
I din't say that system was good, I was merely pointing to your shitty practices.

What compiler are you using (seeing as conio.h and Windows.h are non-standard, the functionality will vary from compiler to compiler)?
Microsoft Visual Studio 2008
I was just trying help.
It did compile in my copy of MVSP 2010. It also compiled with other compilers when conio.h was removed, but unfortunately didn't beep in most compiles. Just a sad day, because I like code that makes noise. Using ANSI characters like \a is one of my favorites just to annoy people who run my program.
But I digress the main problem people have with your post is that it wasn't explained at all. It might be bad code practice, but even bad code can look marginally better with awesome documentation.
I have to wonder if Scottas has the problem solved by now because there isn't another post from him.
Thanks for the tip. I am a little new so I do not know much :)
I will now put comments in my code and explain in depth in my posts.
Thanks.
Topic archived. No new replies allowed.