Click here to Skip to main content
15,616,583 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I am using get line to get a string DOB. but when i compile and execute it doesnt take input for DOB but jumps to the function below.
    int userin;
    string name;
    string firstname;
    string lastname;
    string dob;

	bool exit_b= false;   
while(exit_b == false){

    cout<<"\nmake a new customer enter: 1"<<endl;
    cout<<"\nmake a new staff enter: 2"<<endl;

    if(userin ==1)
            cout<<"Enter First Name: "<<endl;

            cout<<"Enter Last Name: "<<endl;
            name = firstname + " " + lastname;

            cout<<"Date Birth: "<<endl; //this thing should take user input
            getline(cin,dob);   //but this thing doesnt take input it jumps to the next part of the code

            Customer customer(name, dob, rand()%10000);
Updated 20-May-15 2:36am
CHill60 20-May-15 8:49am    
Have you tried stepping through with the debugger to try and see what is happening?

Using stream operators with getline(cin, variable) is always a problem because some data remains in the input buffer. So you should remove everything still in there then read your input.
while (getline(cin, dob))
  if (dob != "")
    break;    //we got something
Share this answer
Member 11705534 20-May-15 9:08am    
@Frankie-C i got it now. i cant use cin>> and getline() together. i have to be stable. and use one. i just changed all the cin>> to getline() and it works perfect now. Thanks
An alternative way of flushing the buffer before calling getline
Share this answer
You need to flush the buffer before using getline. Check this[^] for reference!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900