the program exites as soon as it reaches the end but I want it to ask the user if they want to exit or continue again?

Recommended Answers

All 6 Replies

a do while should work for you just make sure you create an if statement so the program exit if the user enter a specific phrase

it will be very easy to answer this question , if you post you code here.
just receive input and check whether user wanna continue or not (i think if will help you).
if the input is for yes then continue i.e. call the function main();

try this:-

ch=getch();
if(ch=='y')
main();

hope this helps you . . .

try this:-

> 1.ch=getch();
> 2.if(ch=='y')
> 3.main();

Please do not do this. Follow Moschops link on loops.
getch() is not standard and so shouldn't be used.
Recursive calls to main() are simply a terrible practice and should never be done.

I have the C++ 2003 standard to hand which states in section 3.6.1, paragraph 3, "The function main shall not be used within a program."

So, do not call main; it's incorrect C++, as well as a bad idea (look up "stack frame" to get an idea of what can go horribly wrong, with "stack overflow") and demonstrates to someone else reading the code that the programmer appears to have been taught BASIC back in the early eighties and now just tries to write BASIC programmes in whatever language they're using.

You can do this by using do while loop, example is as follows:

    char repeat;

    do
    {

        cout<<"Hello World !"<<endl;

        cout<<"\nDo you want to repeat the program(y/n) ?"<<endl;
        cin >> repeat;

    }while(repeat == 'y');
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.