How to run console again and again?

How to run console again and again in C++:

.
.
.
QUESTION: I want to know how to run console again and again rather than just running once?
......................................................................................................
Explanation:
When i run a simple program that asks user to enter a number and prints the next number to it like:
#include<iostream>
using namespace std;
int main()
{
int Num=0;
cout<<"Please enter number\n";
cin>>Num;
cout<<"Next number is: "<<Num++<<endl;
return 0;
}
This will run once and then automatically it'll close the console.
"I don't want to stop here. I want to run the program again without just closing and again running the code" Anyone help me?
...................................................................................................................................................................................................................................................
Last edited on
You can achieve this by using loops, like while for instance.

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

int main()
{
	int Num=0;

	while (1)
	{
		cout << "Please enter number\n";
		cin >> Num;
		cout << "Next number is: " << Num++ <<endl;
	}

	return 0;
}


but as you might imagine, this loop go forever asking fo number ;) (use ctrl+c to exit program). In that case you must think about while condition to break when you want it.

for example:

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
#include<iostream>
using namespace std;

int main()
{
	int Num=0;
	char q;

	while (1)
	{
		cout << "press n key to input number or q to exit program" << endl;
		cin >> q;

		if (q == 'n') // you can use switch for more options
		{
			cout << "Please enter number\n";
			cin >> Num;
			cout << "Next number is: " << Num++ <<endl;
		}
		else if (q == 'q')
		{
			break;
		}
		else
		{
			cout << "unknown option! choose again" << endl;
		}
	}

	return 0;
}
Last edited on
tath:
Thank you. . . But you didn't understand what i am asking. My intention is not to run this code. It is just an EXAMPLE to explain my issue. I want to run any source code after completing once. Like people use goto statement to go to the top again intead of closing console. I want any other method?
I have no idea what are you trying to say.

My intention is not to run this code
Then what is?
I want to run any source code after completing once
You mean.. what? An exe?
Like people use goto statement to go to the top again intead of closing console
Well, while(1) is doing almost the same thing.. and you can use goto, too.

I don't want to stop here. I want to run the program again without just closing and again running the code" Anyone help me?
Open console (cmd.exe) and then run your program within it, and it wont close
Once the program is finished, its going to end. So, you should have something which will keep the main process alive.. options can be thread based and multi-process based.

- for the first, once your program starts, its in the main thread. Every process has a single thread which is a main thread if its not a multi-threaded application. Now from the main thread, spawn as many threads you want and whatever code you want. Once you want to exit, exit the main thread. look for pthreads (pthread_create etc).

- in process based, you can spawn multiple process from the main process and execute whatever code you want. you can look for fork system call or in windows, createprocess api.

Topic archived. No new replies allowed.