cin - reading empty line;

This is a discussion on cin - reading empty line; within the C++ Programming forums, part of the General Programming Boards category; Hi, I would like to read an empty line. I would like an effect which is similar to: Code: cout ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    cin - reading empty line;

    Hi,
    I would like to read an empty line. I would like an effect which is similar to:
    Code:
    cout << "Press enter to continue."
    getchar();
    But I would like to do it with cin stream. Is it possible?
    Best regards.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    cin.get();
    Woop?

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    21
    You could also put
    Code:
    system("PAUSE");
    I have heard this shunned some times though. I do this, but I am a noob. Just thought that might help. The effect you get from this is
    "Press any key to continue..."
    Make sure to put it on its own line though.
    I only offer this because some compilers (like my own) does not use the
    Code:
    cin.get();
    I don't know why...

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    shadowsora15 : Really really really bad suggestion there. cin.get() is standard and system() can only be evil. Never ever use system().

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Shadowsora15: that isnt possible. if you #include <iostream> cin.get() will be there. you just have to either "using namespace std;' or put "std::" before cin like so
    std::cin.get();
    it will be there though on any standard compiler.

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by Desolation View Post
    shadowsora15 : Really really really bad suggestion there. cin.get() is standard and system() can only be evil. Never ever use system().
    system() is standard too, only the "pause" part is not.

    However, I see nothing wrong with using system("pause") in some situations.

    If you don't need portable code then it's a method that is simple and works as expected. You guys mentioned cin.get() but you didn't mention how if your stream is in a fail state cin.get() will fail or if there are leftover chars in the stream, the program will just continue on.

    Another concern is security, but if someone has replaced pause on your system, then they could just have easily replaced your program. Besides, if someone is replacing programs on your system, it's already too late.

    Finally, efficiency is the last criticism I hear, "you're spawning a new process to pause your current process", ok, but I ask, how efficiently do I need to pause my program? The tiny bit of resources used to pause my program is a small price to pay for the ease of use, especially for someone new to programming.

    I would say there are no absolutes, do what's best for a given situation, which sometimes means using system() or gotos or macros or non-portable code
    Last edited by Darryl; 05-31-2007 at 03:03 PM. Reason: spelling

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    There's an FAQ on this topic: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    system() can only be evil.
    Not nessesarily. You could pass system() a NULL pointer. Though what good that would do unless you were intending to call system() later on I don't know.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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, 07:39 AM
  2. reading a file line by line and char *
    By odysseus.lost in forum C Programming
    Replies: 8
    Last Post: 05-31-2005, 10:47 AM
  3. reading file from second line
    By axon in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2003, 05:18 PM
  4. reading a file line by line
    By marsudhir in forum C Programming
    Replies: 1
    Last Post: 04-16-2003, 09:14 AM
  5. Reading a line from a txt file
    By Nippashish in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2002, 06:22 PM

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