CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2004
    Location
    Texas, earth, Mecca, soul
    Posts
    58

    _ENDing a program...

    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
    "Nay, you love this present life!"

    Save your keystrokes!

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: _ENDing a program...

    I think you are looking for exit(). It terminates your program when called.

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

  3. #3
    Join Date
    Nov 2004
    Location
    Texas, earth, Mecca, soul
    Posts
    58

    Exclamation Re: _ENDing a program...

    (Kheun)

    Thank-you for your reply.
    "Nay, you love this present life!"

    Save your keystrokes!

  4. #4
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: _ENDing a program...

    A little modified version can be like

    Code:
      char szText&#091;128&#093; = {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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured