getline(cin, ) stops code

I'm trying to get the following functions to work where it prompts the user for a string, checks whether that string is an entry in a preceding vector, and if not reprompts the user for a string that is until they provide one. It works fine if a matching string is entered.

But when an invalid entry is received, it displays the reprompt option and asks for a new input, then does nothing. The code stops and seems to ask for another input, and when you give it one it displays the reprompt dialogue again whether you supply it a valid entry or not, and never stops repeating that.

I've had some issues with getline before, but I can't imagine what's causing this one. I'm using cin.ignore to clear the cin stream before every time I use getline.

The vector given is populated with 8 different strings.

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
32
33
34
bool check_valid_restaurant(const vector<string> restaurants, string target_restaurant)
{
	bool valid_restaurant = false;

	for (int i = 0; i < restaurants.size(); i++)
	{
		if (restaurants[i] == target_restaurant)
		{
			valid_restaurant = true;
			return valid_restaurant;
		}
	}
	return valid_restaurant;
}

void remove_restaurant(vector<string>& restaurants)
{
	string target_restaurant;
	bool valid_restaurant = false;

	cout << "Prompt";

	cin.ignore(INT_MAX, '\n');
	getline(cin, target_restaurant);

	valid_restaurant = check_valid_restaurant(restaurants, target_restaurant);
	while (valid_restaurant == false)
	{
		cout << "Reprompt";
		cin.ignore(INT_MAX, '\n');
		getline(cin, target_restaurant);
		valid_restaurant = check_valid_restaurant(restaurants, target_restaurant);
	}
}
Last edited on
Topic archived. No new replies allowed.