Thread: cin question....

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    28

    Question cin question....

    hi all,

    i have a question is there a way to make cin to read in all characters... i just use it straight out (ie cin >> ch; )

    it never grabs the \t \n and other such characters....

    i want to read in a char at a time from a file, and if

    ch == '\t' to print 4 spaces instead of putting a tab.

    any help would be appreciated.
    curiousity killed the cat, but it
    makes for one hell of a programmer.

    Alien.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Use the get function to read single characters:
    file.get ( ch );
    get doesn't have a delimiting value, so it will happily read whitespace.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    if you wish to have a delimiting value, or a limiting size, then getline() can be very useful, too. Beware, however, that there are some special issues when preceding a call to getline by any of the other cin methods.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >that there are some special issues
    Those issues being the same behavior as scanf, cin will leave a newline in the input stream and getline will read the newline and end. The fix for this is either
    cin.ignore();
    or
    while ( getchar() != '\n' );

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple cin question
    By verbity in forum C++ Programming
    Replies: 25
    Last Post: 05-09-2007, 03:02 PM
  2. CIN Input count question?
    By kamran in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2006, 04:06 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. cin Help...Sort of a noob Question?
    By Krak in forum C++ Programming
    Replies: 9
    Last Post: 01-26-2003, 01:23 PM
  5. cin question
    By joebudden in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 07:38 PM