User to exit the program

Oct 6, 2012 at 8:20pm
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
Oct 6, 2012 at 8:23pm
The program ends when execution leaves the main function (either by an explicit return or by reaching the end of main).
Oct 6, 2012 at 8:33pm
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;"
Oct 6, 2012 at 8:40pm
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";
}
}
Oct 6, 2012 at 10:51pm
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()
Oct 7, 2012 at 12:05pm
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
Oct 7, 2012 at 3:04pm
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);//
}
Oct 7, 2012 at 5:25pm
yeah like that /\
Last edited on Oct 7, 2012 at 5:26pm
Oct 8, 2012 at 10:08am
Thats it.

Thank you so much guys for all your help

:)
Topic archived. No new replies allowed.