Results 1 to 2 of 2

Thread: cin.get() and cin.ignore() in C++

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    8

    Question cin.get() and cin.ignore() in C++

    Hello .

    I've literally just started trying to teach myself C++ using Microsoft Visual C++, so please go easy on me, as I know hardly anything yet :P. I know a little more about VB.

    I understand that using cin>>[variablename] will put whatever the user types before pressing enter into the variable, but I was curious about the cin.get(). Apparently it gets the Console to stop and makes you press Enter before proceeding to the next line. But when I've tried it, it appears to need multiple cin.get() functions according to however many characters there are or the program will simply close. It's really odd - can anyone explain to me what it's actually doing, and if there's a better way besides system ("PAUSE") to hold the Console?

    I'm also curious about cin.ignore() - apparently it throws away any characters in an input that don't belong to a certain data type. I had an experiment with this - when I typed in "56f" for an Int variable and used cout<< to print it, it printed "56" and then used up the cin.get() below and closed the program. I put in another cin.get() and it works. I type in "56fg" and this time it closes again.

    Sorry if this sounds stupid, I really am a complete beginner here, so any help in explaining these functions to me - and any terms that I may have misused - would be really appreciated .

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: cin.get() and cin.ignore() in C++

    You should read some introductory article on c++ streams, such as this one: http://www.cprogramming.com/tutorial/c++-iostreams.html
    I haven't personally read that specific article, but it seems OK.

    std::cin is basically just a very special stream, an instance of std::istream. You can see exactly what the get and ignore methods does on that page.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width