Click to See Complete Forum and Search --> : _ENDing a program...


Mansoor8
December 7th, 2004, 06:36 PM
Hello:
I have been researching how to end a program, giving the user an opportunity to end the program, or myself in the coding process via a FOR loop to do so. I have found examples involving "end" and "_end" or "cend" (?) but am unsure how to implement it correctly, what it will do exactly, and when it will do it. I need to integrate this into my code. Please explain with a commented ( //--) example if possible...

Thank-you in advance,
Mansoor

Kheun
December 7th, 2004, 06:51 PM
I think you are looking for exit(). It terminates your program when called.


int main()
{
for(int i = 0; i < 100; ++i)
{
if(i == 50) exit(0); // Terminates your program when i reaches 50
}

return 0;
}

Mansoor8
December 7th, 2004, 07:15 PM
(Kheun)

Thank-you for your reply. :wave::p:wave::thumb:

Ejaz
December 7th, 2004, 10:14 PM
A little modified version can be like


char szText[128] = {0};

for (int i = 1; i < 10000; i++) // A long loop
{
sprintf(szText , "%d) Hello World\n" , i++);
printf(szText);

if (kbhit())
{
char key = _getch();

if ('q' == key)
break;
}

}


Here, the loop will be terminated, if it gets complete or user press "q" key.