Help me out with cin.getline()

This is a discussion on Help me out with cin.getline() within the C++ Programming forums, part of the General Programming Boards category; Hello, As we all know that cin alone is not able to take full sentence, it can take only single ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    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, 11:01 AM
  3. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 08:47 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM

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