cin stream

This is a discussion on cin stream within the C++ Programming forums, part of the General Programming Boards category; I am new to c++, so I am still trying to figure out the basics. When using the 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
    21,445
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,445
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01: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, 04: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, 04:00 PM

Tags for this Thread


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