Console Input with std::getline()

Hi, I am currently writing a program that will take in patient information that will determine the priority in queue.

I am writing the console application that will gather this information. I am having trouble with retrieving the information.

I am asking for the patients name, type of ailment, the severity, time criticality, and contagiousness.

Each patient can have more than one ailment and I am asking that the type of ailment to be left blank when all ailments have been inputted into the console. However when I leave the ailment field blank (press enter) the console is not registering the \n and continuing with the code. I do not know what I am doing wrong.

I am also having trouble have the information being entered on the same line as the prompt. Currently it is not doing that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
while(user_selection)
	{

		if (user_selection == 1)
		{
			std::cout << "Enter patient name: ";
			std::getline(std::cin, name);

			do
			{
				std::cout << "\tEnter ailment (leave blank when done): " << std::endl;
				std::getline(std::cin >> std::ws, ailment);
				if(ailment.empty())
				{
					break;
				}

				std::cout << "\tEnter severity: " << std::endl;
				std::cin >> severity;

				std::cout << "\tEnter time criticality: " << std::endl;
				std::cin >> time_criticality;

				std::cout << "\tEnter contagiousness: " << std::endl;
				std::cin >> contagiousness;
			}
			while (!ailment.empty());
		}

		return 1;
	}


Update: I have figured out why the console was not registering the empty line input in the console. However I am still having trouble with having the input on the same line as each prompt.
Mixing std:: cin >> operator with std::getline can leave a stray '\n' in the input buffer after the cin >> call, which std::getline then consumes (instead of waiting for more user input).

Add a std::cin.ignore(); call after line 25, within the while loop.

If you want input to be on the same line for some aesthetic reason, don't put std::endls at the ends out the cout lines.
Last edited on
Thank you so much! I can't believe it was as simple as just removing the std::endl;

Also I created my own flush_buffer function that helped with the empty line input.
Topic archived. No new replies allowed.