What is the difference between cin.get(ch) and ch=cin.get()?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mahesh Baria
    New Member
    • Jan 2011
    • 3

    What is the difference between cin.get(ch) and ch=cin.get()?

    What is diffrence between cin.get(ch) and ch=cin.get()?He re ch is a char variable.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Difference is that cin.get(ch) returns an istream object, while cin.get() returns the read character as an integer.

    First version allows you to more easily read the stream in a loop because it returns an istream object which got boolean conversion so you can do the following(e.g):

    Code:
    char c;
    while(cin.get(c))
    {
       //do something
    }
    , instead of having to do:

    Code:
    char c;
    while(cin.fail()==false)
    {
       c = cin.get();
       //do something
    }
    Last edited by Savage; Jan 21 '11, 08:08 AM. Reason: Fixed small typo: Thanks tdlr :)

    Comment

    • tdlr
      New Member
      • Jan 2011
      • 22

      #3
      There's a small typo in line 2 of your code, Savage. It should be:
      Code:
      while(cin.get(c))

      Comment

      Working...