How to Re run a program without exiting the prompt

Hello, I have a question that I can't seem to find anywhere online (probably because its too wordy when I search in google). Anyhow, I want to know how I can return to the top of my program once its done executing. Example:

When I run my program, its bug free, but in the command prompt we get the "Press any key to continue..." at the bottom of the prompt. Instead, I would like start the program over again without opening and closing the debugger. Can someone help me out with the block of code I need to enter at the bottom for that? I know this is simple, I just can't remember. Thanks

-Dean
Put the whole thing in a while (true) loop.

1
2
3
4
5
6
7
8
9
int main()
{
        while (true)
        {
                // code...
        }

        return 0;
}
Last edited on
That did nothing. It still says press any key to continue instead of going back to my initial prompt of asking a user to input a max number.

I want to get my program to, after it is done running. start over as if I just opened it the first time. not say press any key to continue.
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
#include <iostream>
#include <cstdlib>

void clear();

int main()
{
    while (true)
    {
        int number = 0;
        std::cout << "Enter number: " << std::flush;
        std::cin >> number;
        std::cin.ignore();
        std::cin.get();
        clear();
    }

    return 0;
}

void clear()
{
        #if defined(_WIN32)
                system("cls");   //On windows, do this
        #elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
                system("clear");//On Linux/OS X/BSDs
        #else                   //For an unrecognized OS, just print 50 newlines
                for (int i = 0; i < 50; ++i) std::cout << std::endl;
        #endif
}
Last edited on
Topic archived. No new replies allowed.