this is my style to repeat program
int repeat;
cout << "continue: type 1 or to exit type 0: ";
cin>>repeat;
if (repeat != 0)
{
main();
}
// main() continues but it doesnt close the previous program
//i want to do something like main().close();
// Header files here
#include <iostream>
int main()
{
bool repeat{ true }; // <--- No need for a globl variable.
do
{
/*
your code here
*/
cout << "continue: type 1 or to exit type 0: ";
cin >> repeat;
} while (repeat);
return 0;
}
I know it's unsatisfying to get an answer "because they said so", but the ISO/IEC international committee's rationale is a consequence of long-standing convention and the other requirements imposed (or not imposed) upon the main function and the implementation.
While I personally detest using recursion in this way, you could create your own function and then have that function call itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void myfunc ()
{ // Some code
int repeat;
cout << "continue: type 1 or to exit type 0: ";
cin>>repeat;
if (repeat != 0)
{ myfunc();
}
}
int main ()
{ myfunc ();
return 0;
}
The reason I detest using recursion in this manner is that every time you repeat the code, your add another stack frame to the stack. If the code is repeated enough times, you could run out of stack space. You can avoid this, as others have said, by using a conventional loop.