Thread: cin stream

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    31

    cin stream

    I am new to c++, so I am still trying to figure out the basics.

    When using the cin stream to input information from the console, why does the program always terminate when I press the Enter key?

    What is the exact function of the cin stream?


    Or just in a simple "Hello World" program, why do I have to create some sort of pause to stop the program from terminating before I can even see the output, "Hello World"?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RaisinToe
    When using the cin stream to input information from the console, why does the program always terminate when I press the Enter key?
    Because you have not read the FAQ

    Quote Originally Posted by RaisinToe
    What is the exact function of the cin stream?
    To allow you to read from standard input, which in this case means keyboard input to the console.
    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
    Jan 2009
    Posts
    31

    Smile

    Oh . . . thank you.

    Too often, in other web sites, I have gone to the FAQs, and they have nothing that I care to know. But that's not the case here.


    Hmm, making a simple pause still looks like a lot of work.

    There was one command I saw that created a simple pause. It was this:

    Code:
    char pause[100];
    cin.getline(pause,100);
    although it still has to use a premade function getline.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RaisinToe
    although it still has to use a premade function getline.
    Of course. Since you are using cin, you are bound to use some of the functions supplied by std::istream (i.e., the std::cin's type).

    That said, instead of creating a temporary array, it would be easier to write:
    Code:
    cin.ignore(100, '\n');
    cin.get();
    Or more properly, you could #include <limits> and write:
    Code:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    However, better ways include running the program from the command prompt or from an IDE that pauses the program for you.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Discard wrong data from stream
    By mesmer in forum C Programming
    Replies: 7
    Last Post: 11-16-2008, 02:30 AM
  2. Some bugs which i can't find
    By Lincoln_Poh in forum C++ Programming
    Replies: 7
    Last Post: 09-18-2008, 08:11 PM
  3. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 05:15 PM
  4. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM
  5. eof member function on the stream cin
    By stimpyzu in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2002, 05:00 PM

Tags for this Thread