0

I have a strange problem here. I'm getting 2 strings as input using the code below:

std::string input1, input2;
std::cout<<"Please enter name:";
std::getline(std::cin, input1);
std::cout<<"Address: ";
std::getline(std::cin,input2);

The issue I'm having is that instead of displaying one at a time, both display at once, so the output looks like this:

Please enter name: Address:

As a result neither string is getting any input.

Any ideas what the issue might be?

John
  • 599
  • 5
  • 10

2 Answers2

3

Try using cin.ignore() before getline // ignores \n that has been left after user pressed enter key

RigidBody
  • 660
  • 2
  • 10
  • 23
0

Don't use

cin.ignore();

Because in loop it will ignore you required input as well. Use

cin>>ws;

Before getline. ws means white space this will ignore white space in your program and the code will work fine.

Ahsan
  • 392
  • 3
  • 17