User to exit the program

Hi guys and Girls

First of all i want to say that i am very new to c++ as i have just started learning it at college. My assignment will be issues soon where i need to design a cinema ticket booking system. I have started my program, but i am breaking it down into small chunks.

So i have my first very simple question.

I have created a menu of 5 options using an IF statement, my 5th option is for them to exit the program using an integer. What is the code i need to make them exit the program

Thanks in advance

Michael
The program ends when execution leaves the main function (either by an explicit return or by reaching the end of main).
no code to stop program.
agree with athar "The program ends when execution leaves the main function".

if you looping but want to stop looping if condition reach, you can use "break;"
int main()
{
int choice;
cout << "1.Hello world \n"<<"...\n"<<"5. Exit the program\n";
cin>> choice;
if (choice == 1)
{
cout << "Hello world";
}else if (choice==5)
{
return 0;
}else {
cout << "no in the list";
}
}
ikra u r dead wrong there r many ways to do it. an easy way is where u want the program to exit early is return anInt; there is also (in the cstdlib library) exit(int) and abort()
Thanks guys,

Aramil of elixia could you give me an example of your suggestion in some code, as I don't really understand it.

Thanks
else if (choice==5)
{
return 0;
} this code exit the program when user press 5 .

Are you looking somthing like this ?

also you can end the program with exit() function that is part of "cstdlib " library:


#include <cstdlib>//...
//...
else if(choice==5)
{
exit(EXIT_FAILURE);//
}
yeah like that /\
Last edited on
Thats it.

Thank you so much guys for all your help

:)
Topic archived. No new replies allowed.