Thread: Help me out with cin.getline()

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Help me out with cin.getline()

    Hello,
    As we all know that cin alone is not able to take full sentence, it can take only single word. My problem is when i use cin.getline(s,20) the program runs and there's no error but when i try to take the input it doesn't take
    Without taking any input it moves to the next instruction!
    Please tell me why is this happening? i tried using fflush(stdin) but still the same problem is coming. Please help me out!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First, it's better to use std::getline instead.
    Fflush(stdin) is wrong and undefined. Never use it.
    And as for the question, the FAQ has an entry for that I think. Otherwise the board has a suggestion somewhere because it's a very frequently asked question.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    As we all know that cin alone is not able to take full sentence, it can take only single word.
    No we do not know that.
    We know that operator >> stops on the white space.
    There are other functions, that could work with input stream and they do not stop on whitespace, instead you can specify the delimiter (end-line or point for example)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Quote Originally Posted by Elysia View Post
    First, it's better to use std::getline instead.
    But in my college it's very very veryyyyyyyyy old compiler its turbo borland c++ 3.0 and in that this method is not supported

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Hey i got the reason i have to write cin.ignore() to clear the buffer it works!!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Tell your college to upgrade and stop teaching students nonsense with old compilers!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 06:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  3. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM