Why does DOS always close after the program completes?

This is a discussion on Why does DOS always close after the program completes? within the C++ Programming forums, part of the General Programming Boards category; I've never acctuelly had this problem before, so I was wondering why the following program always closes right after you ...

  1. #1
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309

    Why does DOS always close after the program completes?

    I've never acctuelly had this problem before, so I was wondering why the following program always closes right after you reach the end of the program (outputing the stored data).
    Here is the code so far:
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <string.h>
    //class
    class NameDataSet
    {
        public:
            char firstName[128];
            char lastName[128];
            int ssNumber;
    };
    
    //getdata function will retrieve info from user in consol
    int getData(NameDataSet& nds)
    {
        std::cout<<"\nEnter your first name:\n";
        std::cin>>nds.firstName;
        
        if 
                ((strcmp(nds.firstName, "exit") == 0)
                ||
                (strcmp(nds.firstName, "EXIT") == 0))
        {
            return 0;
        }
        
        std::cout<<"\nEnter your last name:\n";
        std::cin>>nds.lastName;
        
        std::cout<<"\nEnter credit card number:\n";
        std::cin>>nds.ssNumber;
        
        return 1;
    }
    
    //display data will display all data in the database
    void displayData(NameDataSet& nds)
    {
        std::cout<<"Your name(s):\n" << nds.firstName << " " << nds.lastName << "\n";
        std::cout<<"Your creditcard number(s):\n" << nds.ssNumber << "\n";
    }
    
    int main(int nArg, char* pszArgs[])
    {
        NameDataSet nds[25];
        std::cout<<"Read name/credit card information\n" << "Enter 'exit' for first name to exit\n";
    
        int index=0;
        while (getData(nds[index]))
        {
            index++;
        }
    
        std::cout<<"\nEntries\n";
        for (int i = 0; i < index; i++)
        {
            displayData(nds[i]);
        }
        return 0;
    }
    The book I'm reading right now that I got it from, mentioned it in the first chapter, but since I never experieinced this happening in the first chapter, but never gave a way to solve it. Also, I had never had this problem up until now. Can anybody explain how to stop this from happening?
    To code is divine

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    just place a getch(); or system("pause"); at the end of your program and it wont close until you hit some bottom, or you can make a loop that restarts your program at the end. include conio.h for getch();

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    63
    I just put the following bit at the end of int main()

    Code:
    int closer;
    cout << "Type something to exit! :) \n";
    cin >> closer;
    return 0;

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by Philandrew
    I just put the following bit at the end of int main()

    Code:
    int closer;
    cout << "Type something to exit! :) \n";
    cin >> closer;
    return 0;
    a simple cin.get(); will get the job done, but whatever works.

  6. #6
    Banned
    Join Date
    Oct 2004
    Posts
    15
    On my computer, I had the same problem with Dev-C++ (That Bloodshed program). The FAQ recommends that you do this:

    Add #include <stdlib.h> to the top, and add a system("PAUSE"); to the bottom, like this:
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    int main()
    {
      cout << "This program will stay open until you hit a key \n";
    
      system("PAUSE");	
      return 0;
    }

    This works for me.

  7. #7
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Is there any type of function that I could call after using cin.get() or getch() to return the program to the start of the main function?

    edit:other than goto

    edit2: nevermind, loops are just easier :)
    Last edited by 7smurfs; 10-21-2004 at 08:46 PM.
    To code is divine

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>The FAQ recommends that you do this
    Actually, it doesn't It recommends alternate methods, due to security vulnerabilities inherent in the system() call - anyone can write a program, name it 'pause.exe' or 'pause.com', and replace the one that came with your computer.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    to answer every question in this thread:

    >>Why does DOS always close after the program completes?
    it shouldn't because it never opens when the program starts...

    1) you're running in a console window, not DOS... the window you're using is actually the opening for command-line input to your OS...

    2) use cin.get(); -> system("anything") is not the safest way to go because of what Hunter2 said and getch() is not as portable (although it is nicer, because you really can press pretty much any key)

    3) <stdlib.h> should be <cstdlib>


    edit: I just found negative thread ratings
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56
    As far as I've seen, programs will do this because it isn't waitin for anything. I usually just put a cin>>a;, where 'a' is a char. this makes the computer pause before closing, giving the user a chance to read what it just printed out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. DOS Program Question
    By PriMaL in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2002, 06:02 PM
  3. how to command to close a dos window?
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 07-19-2002, 02:04 PM
  4. using spawnl for a dos program
    By ronin in forum C Programming
    Replies: 6
    Last Post: 06-30-2002, 09:34 PM
  5. initialising a DOS program from a C enviroment
    By Robert_Ingleby in forum C Programming
    Replies: 5
    Last Post: 03-07-2002, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21