Thread: Keep console open

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    Keep console open

    In the following listing I have used cin.get() to keep the console window open when I run the .exe file - it has worked before for me but in this program it doesn't - the console shuts immediately after outputting the list - I just know this is going to be something obvious so sorry in advance!!

    Code:
    #include <algorithm>
    #include <iostream>
    #include <istream>
    #include <ostream>
    #include <vector>
    
    
    using namespace std;
    
    int main()
    {
      vector<int> data;     // initialized to be empty
      int x(0);
    
      // Read integers one at a time.
      while (cin >> x)
        // Store each integer in the vector.
        data.push_back(x);
    
      // Sort the vector.
      sort(data.begin(), data.end());
    
      // Print the vector, one number per line.
      for (vector<int>::size_type i(0); i != data.size(); i = i + 1)
      {
          cout << data.at(i) << '\n';
      }
    
    cin.get();

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try using cin.ignore() prior to that cin.get() so as to discard leftover characters from the input buffer (e.g., the enter you used on that last line to trigger EOF). Note that cin.ignore takes arguments such that you can ignore more than one character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    That is because your program has trailing newline (enter) characters. Read How to keep my console window open-FAQ
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    The cin.ignore() didn't make any difference I am afraid.

    But

    printf ( "Press any key to continue . . ." );
    getch();

    return 0;

    did work.

    It seems a bit much to have to include 2 more libraries to use printf and getch just to keep that window open; there must be an easier way - or is all C++ so convoluted?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JayCee++
    The cin.ignore() didn't make any difference I am afraid.
    Since you had to trigger EOF to get the output in the first place, the stream should no longer be in the good state. Therefore, use cin.clear() prior to cin.ignore() to clear the error state.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Thanks that works, so if I put
    cin.clear();cin.ignore();cin.get();
    as the last line of my little progs the console window will always stay open till I press a key!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not necessarily, because there may be more characters left on the input buffer.

    The solution I recommend is to simply run your command line program from a command prompt window
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by AndrewHunter View Post
    That is because your program has trailing newline (enter) characters. Read How to keep my console window open-FAQ
    Hmm...it appears we agree again.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how too keep the console page open?
    By arian in forum C++ Programming
    Replies: 10
    Last Post: 10-07-2010, 08:31 AM
  2. open a console from a GUI in c++
    By lollobrigido in forum Linux Programming
    Replies: 29
    Last Post: 01-18-2008, 08:31 AM
  3. Keeping the console open.
    By foogoo in forum C++ Programming
    Replies: 5
    Last Post: 10-30-2002, 07:43 PM
  4. keeping console window open
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2002, 07:15 PM
  5. Keeping the console window open
    By Adock in forum C Programming
    Replies: 3
    Last Post: 01-31-2002, 05:15 AM