Problem using getline after cin in C++ Program


When you need to input numbers and strings you’ll certainly use cin and getline functions in C++. If you are beginning programmer, you must face this weird problem. Look at the code below:

#include<iostream>

#include<string>

using namespace std;


int main(){

int id, age;

string name, address;

 

cout<<”Enter ID : “; cin>>id;

cout<<”Enter Name: “; getline(cin,name);

cout<<”Enter Address : “; getline(cin, address);

cout<<”Enter Age: “; cin>>age;

}

The code looks fine and there is no error on reading it. It must prompt user for a number as ID, then get name and address and finally get age. Why I used getline() instead of cin for name and address too?

Well because cin can accept only one word for string variable. cin will stop after it reaches any delimiters such as space or newline. If you need to make user type many words, you should use getline(cin,string_variable).

the getline(cin,string_variable) will obtain user input through standard input device (keyboard) and store it in supplied string_variable. So, there should not be any problem on above code. It is absolutely correct! But when you run the code following output is produced:

Enter ID: 23

Enter Name: Enter Address : Kathmandu

Enter Age : 45

Press any key to continue . . . 

Suppose user entered 23 for ID and 45 for Age but the program did not let user to enter Name.

Now lets just change the order at which the values are obtained from user:

#include<iostream>

#include<string>

using namespace std;

int main(){

int id, age;

string name, address;

cout<<”Enter Name: “; getline(cin,name);

cout<<”Enter Address : “; getline(cin, address);

cout<<”Enter ID : “; cin>>id;

cout<<”Enter Age: “; cin>>age;

}

Now, when you run this code the behavior is perfectly normal. It lets user to enter value for each prompt and program runs fine!

Enter Name: Suresh Khanal

Enter Address : Kalanki, Kathmandu

Enter ID: 23

Enter Age: 43

Press any key to continue . . .

That means in C++ programs you can’t use getline() after cin.

Why can’t you use getline after cin?

cin>> leaves the newline character (\n) in the iostream.  If getline is used after cin>>, the getline sees this newline character as leading whitespace, thinks it is finished and stops reading any further.

Solution?

There can be two workarounds. One you’ve already seen.

Change the input order so that getline won’t come after cin. But it might not always be welcomed. After all, you can’t always go against nature.

The second method is to empty cin buffer. Create a dummy string variable and read whole line into dummy variable from cin. Then prompt for value for your program. Or the other way can be to use getline two times. On first time it will pull out everything left (including spaces and new line character) from cin and the second getline will input fresh content from input device.

You have already seen the first solution in above code. Now, lets see by using getline two times:

#include<iostream>

#include<string>

using namespace std;

int main(){

int id, age;

string name, address;

cout<<”Enter ID : “; cin>>id;

getline(cin,name);
cout<<”Enter Name: “; getline(cin,name);

cout<<”Enter Address : “; getline(cin, address);

cout<<”Enter Age: “; cin>>age;

}

 

Now, it works fine. Look at following output, where I’ve entered Suresh Khanal for Name, Kalanki, Kathmandu for address :

Enter ID : 23

Enter Name : Suresh Khanal

Enter Address : Kalanki, Kathmandu

Enter Age : 32

Press any key to continue . . .

Voila! Happy Learning!

Tagged As: , , , ,

3 Responses to “Problem using getline after cin in C++ Program”

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Review this blog on Bloggers.com