Thread: cin.get();

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    cin.get();

    Is this command: cin.get(); compiler specific?? because I have been hunting high and low for a way to make my screen oause without using system("pause"). I have tryed all the following:

    cin.get();
    cin.sync();
    get.char();
    getch();

    the only one that works is getch(), but I get warnings about using it, I am using Microsoft Visual 2005 beta compiler now, and on my old DevC++ compiler which I still have as backup, all the above work apart from cin.get. What should I do?
    I have looked at the FAQ and googled it, I know I can use getch() for now, but there must be another command I can use apart from system("pause"), which is frowned apon

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    std::cin.get() is part of the C++ standard library.

    As for your answer, it can be found by searching, and might result in you finding a thread like this one.
    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
    Jun 2005
    Posts
    6,815
    get() is a member function of basic_istream, so cin.get() is legal. Similarly for cin.sync(). get() returns the next byte in the stream. cin.get() won't do what you want though, as it treats the standard input like a file. In other words, if the user types ABC<enter> it will return 'A' [or, more accurately, the integer value associated with 'A'] after the <enter> is hit. If cin.get() is called again, with no other operations that get input from cin, the next call will return 'B' .

    get.char() is meaningless, as char is a keyword.

    getch() will do what you want, but is compiler dependent. Unfortunately, there is no equivalent function supported in either the C or C++ standards. The reason for this is that such functions assume a computer with a keyboard to get keystrokes from, and both C and C++ are designed to work on machines that don't necessarily have a keyboard.

    system("pause"); is not specifically frowned upon because it is compiler dependent. It only works with Microsoft operating systems under certain conditions (eg if the user installs a different command interpreter it won't necessarily work). In other words, depending on how the system is set up, it won't work ..... which is not exactly a good idea if you are providing software to people and can't control how they set up their operating system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 10:33 PM
  2. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  3. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 11:44 AM
  4. cin.get() aint working.
    By Blips in forum C++ Programming
    Replies: 19
    Last Post: 01-17-2005, 06:55 PM
  5. curiosity about cin.get() and cin.getline()
    By ssjnamek in forum C++ Programming
    Replies: 18
    Last Post: 11-30-2003, 01:26 AM