Click here to Skip to main content
15,881,588 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.
C++
    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;
    cin>>userin;

    if(userin ==1)
        {
            cout<<"Enter First Name: "<<endl;
            cin>>firstname;

            cout<<"Enter Last Name: "<<endl;
            cin>>lastname;
            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);
            customer.print_detail();
        }
Posted
Updated 20-May-15 2:36am
v2
Comments
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.
I.e.
C++
while (getline(cin, dob))
  if (dob != "")
    break;    //we got something
 
Share this answer
 
Comments
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
cin.sync();
 
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