Need Advice for Getline() function of strings

This is a discussion on Need Advice for Getline() function of strings within the C++ Programming forums, part of the General Programming Boards category; Dears, I am facing a strange problem at my end. I am using getline(cin,name) function for reading strings from console. ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Need Advice for Getline() function of strings

    Dears,
    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:
    Code:
    class testClass
    {
    private:
    int firstvalue;
    string name;
    char secondvalue;
    double thirdvalue;
    public:
    void settingtestClass();
    void print();
    testClass();//default constructor
    };
    int main()
    {
    testClass one;
    while(1){
    one.settingtestClass();
    cout<<"Output--------------------------------- "<<endl;
    one.print();
    cout<<"-------------------------------------------"<<endl;
    cout<<endl;
    }
    return 0;
    }
    testClass::testClass()
    {
    firstvalue = 0;
    thirdvalue = 0.0;
    secondvalue = 'a';
    name="";
    }
    void testClass::settingtestClass()
    {
     
    cout<<"enter the name"<<endl;
    getline(cin,name);
    cout<<"enter any value of int data type"<<endl;
    cin>>firstvalue;
    cout<<"enter any value of character data type "<<endl;
    cin>>secondvalue;
    cout<<"enter any value of int data type"<<endl;
    cin>>thirdvalue;
    }
    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
    Posts
    535
    The problem is that

    Code:
    cin>>thirdvalue;
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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

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