Thread: Why does it return to the operating system after program is completed??

  1. #1
    Unregistered
    Guest

    Why does it return to the operating system after program is completed??

    I have a prob...I am a n00b and I am writing a very simple program. But when I test the program it has me enter 2 numbers just the way I want it to but then when it is supposed to add them it just closes the program! It does that on all the examples for tutorials on this site too! Can someone pleaaaazzz help????

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Place a request for input just before the return statement in your program. The program will wait for input and will not close:
    Code:
    #include <iostream>
    
    int main()
    {
      std::cout<<"Hello, World!\n";
      std::cin.get();
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    ur u could use gletch instead of cin.get i think
    Email: [email protected] || AIM: MisterSako || MSN: [email protected]

    -the shroom has spoken

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >ur u could use gletch instead of cin.get i think
    getch is non-standard, the OP may not have it.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Jeter
    Guest

    kiss

    If the OS is Windows, the console window may be set to close automatically when the program finishes... If this is the case, I do not know the current method to change it. In windows 3.1x you could edit the pif for the program. The checkbox was called close on exit. MS has hidden this deeply now, I think.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: kiss

    Originally posted by Jeter
    If the OS is Windows, the console window may be set to close automatically when the program finishes... If this is the case, I do not know the current method to change it. In windows 3.1x you could edit the pif for the program. The checkbox was called close on exit. MS has hidden this deeply now, I think.
    Under Win98 "Close on exit" still exists. Just go Properties->Program->Close on exit.

    Alternatively, just open a console window and navigate your way to the desired directory manually, and run your program from there. That way, when your prog finishes, you'll be back at the prompt again. On my setup, I have a hotkey in my IDE to get me a command console prompt, starting in the same directory as the file I'm editing (Ultra Edit, but others will do this too).

    If anyone's interested in this method, here's the details I used to setup the shortcut :>
    Command: C:\WINDOWS\COMMAND.COM /k doskey
    Working Dir: %p <--- or whatever your IDE says is the current directory variable.

    This is a Win98 example. Under NT and up (I believe), you'd need to run CMD.EXE instead.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  3. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM