Thread: Need Advice for Getline() function of strings

  1. #1
    Registered User
    Join Date
    Nov 2011

    Need Advice for Getline() function of strings

    I am facing a strange problem at my end. I am using getline(cin,name) function for reading strings from console. According to my Code Hierarchy i enter data for one user and then the program again asks me to enter the data for the next user.

    When my program reads the data for the first time, it takes input correctly but when it asks for the second user's data. The program just bypasses the getline(cin, name) statement as if it is not there.

    Kindly advise what is wrong with it. I am sharing the Code as below:
    class testClass
    int firstvalue;
    string name;
    char secondvalue;
    double thirdvalue;
    void settingtestClass();
    void print();
    testClass();//default constructor
    int main()
    testClass one;
    cout<<"Output--------------------------------- "<<endl;
    return 0;
    firstvalue = 0;
    thirdvalue = 0.0;
    secondvalue = 'a';
    void testClass::settingtestClass()
    cout<<"enter the name"<<endl;
    cout<<"enter any value of int data type"<<endl;
    cout<<"enter any value of character data type "<<endl;
    cout<<"enter any value of int data type"<<endl;
    void testClass::print()
    cout<<"first value = "<<firstvalue<<endl;
    cout<<"name = "<<name<<endl;
    cout<<"secondvalue = "<<secondvalue<<endl;
    cout<<" thirdvalue = "<<thirdvalue<<endl;

  2. #2
    Registered User
    Join Date
    Mar 2010
    The problem is that

    doesn't take the newline (after you enter a number and hit enter) from the input. getline() stops when it sees a newline, so when you call getline() the second and subsequent times through the loop it'll just read the newline you had from the last loop.

    It doesn't affect the "cin >> value" lines because they ignore whitespace.

    There are a few ways you could solve it -- for example you could put another getline() after the cin<<thirdvalue. Whatever you do, I'd recommend you do it for all your "cin << value" lines - perhaps write a little function cinAndEatNewline to keep it tidy

  3. #3
    Join Date
    Oct 2007
    Indent your code.
    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. Need help with getline function
    By chickenlittle in forum C++ Programming
    Replies: 7
    Last Post: 10-13-2010, 01:04 PM
  2. Getline and strings...
    By sting777 in forum C++ Programming
    Replies: 11
    Last Post: 06-08-2010, 12:21 PM
  3. Replies: 3
    Last Post: 12-23-2009, 06:38 AM
  4. Is there anyway to use the getline function with strings?
    By Terranc in forum C++ Programming
    Replies: 4
    Last Post: 12-26-2002, 11:02 AM
  5. getline() function
    By unanimous in forum C++ Programming
    Replies: 4
    Last Post: 12-25-2001, 07:58 PM