0

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?

6
Contributors
6
Replies
32
Views
4 Years
Discussion Span
Last Post by new_developer
0

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

0

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 . . .

0

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.

0

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.

0

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');

Edited by new_developer

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.