cin.ignore() & cin.get()

This is a discussion on cin.ignore() & cin.get() within the C++ Programming forums, part of the General Programming Boards category; I am using an old computer science book to work on C++ (old as in from 2000). The book was ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    6

    Question cin.ignore() & cin.get()

    I am using an old computer science book to work on C++ (old as in from 2000). The book was written mostly for use with CodeWarrior and Borland. When I enter the example programs from the book into my compiler (Dev-C++), I find that I have to use cin.ignore() and cin.get() to keep the console window open so that I can see the results of my programs (I got this suggestion from one of the tutorials on the site). I specifically remember that a few years ago when I used CodeWarrior I did not have to do this. Is this an issue that only happens in Dev-C++ or does it happen in other compilers and IDE's as well? Thanks

  2. #2
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    This is not an issue of the compiler or IDE. Instead it is a feature of CodeWarrior. I've encountered such features in some IDEs such as QBasic and the like.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Some IDE's add code to the end of your program to keep the window open, but that only works when you run the code from your IDE. The issue with the console window closing happens everywhere, it is only when the IDE adds the extra code in a few cases that you don't have to worry about it.

    So it's okay to get used to having that at the end of your program so that your code will stay open no matter which IDE you use or how your run it.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    6

    cool

    Thanks for the info I guess I'll just make sure I develop the habit of leaving those lines in, just to make my code more portable

  5. #5
    Covenent Killer
    Join Date
    Jul 2005
    Posts
    26
    yea, i use cin.get() in my programs, i fins its more efficient then the people saying to use system("pause"). I find it to be the next best thing next to a Sleep(x) function
    When I joined the Core, We didnt have any fancy Shmancy tanks! We had Sticks, TWO sticks and a rock for a whole platoon, And we had to share the rock! -- Sarge

    My Other Name Is Warhawk
    They Just Didn't let Me Have That Name...

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    6
    Just curious, why is cin.get() more efficient?

  7. #7
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    well because system("pause") executes a new program, just to pause yours. It also makes your code non-portable.

    you'll find that just about everyone will advise never to use system("pause").
    Last edited by dra; 08-08-2005 at 01:20 PM.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1

    Same problem..still

    So how did you end up solving the problem? I used to use Borland and I'm trying to get back into C++ programming. I use DevC++ and putting cin.get(); at the end of my program doesn't keep the window open. Sometimes it works, but most of the time the window just closes. Is there something I'm missing?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If you are using cin >> to get your input, you have to ignore leftover characters in the stream, otherwise cin.get() just gets those characters. The simplest way to do this is to add a cin.ignore(); above the cin.get(); to ignore the newline character from when the user hits enter.

    You can instead add a cin.ignore(); after every call to cin >>, which will help if and when you use getline. Also, if you want to do good error handling, which most new programmers gloss over, you can #include <limits> and use

    cin.ignore(std::numeric_limits<std::streamsize>::m ax(), '\n');

    which will ignore everything left in the stream, even if the user types in extra stuff that you don't need.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    UK - London
    Posts
    16
    what does cin.ignore do? Simply ignore the code??

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If you call it without any parameters, it ignores a single character in the input stream. If there are no characters in the input stream, it waits for the user to add characters to the input stream, and then ignores the first one.

    If you add parameters like my example, then the first parameter indicates the maximum number of characters to ignore, while the second parameter indicates the terminator to stop at. The terminating character is also removed from the input stream.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    6
    Quote Originally Posted by Daved
    If you are using cin >> to get your input, you have to ignore leftover characters in the stream, otherwise cin.get() just gets those characters. The simplest way to do this is to add a cin.ignore(); above the cin.get(); to ignore the newline character from when the user hits enter.

    You can instead add a cin.ignore(); after every call to cin >>, which will help if and when you use getline. Also, if you want to do good error handling, which most new programmers gloss over, you can #include <limits> and use

    cin.ignore(std::numeric_limits<std::streamsize>::m ax(), '\n');

    which will ignore everything left in the stream, even if the user types in extra stuff that you don't need.
    70f7, I used Daved's suggestion, and it solved my problem. I just had to make sure I got in the habit of using cin.ignore() after every cin statement. good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.ignore and cin.get in C
    By Trafalgar Law in forum C Programming
    Replies: 5
    Last Post: 11-24-2008, 10:34 AM
  2. cin.get and cin.ignore
    By Trafalgar Law in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2008, 10:49 AM
  3. cin.get(); doesn't work even with cin.ignore();
    By calumn in forum C++ Programming
    Replies: 34
    Last Post: 05-14-2006, 12:12 PM
  4. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 12:44 PM
  5. need help with cin.get, or cin.ignore
    By yoyo in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2003, 02:14 AM

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