Looping issue

Hi everyone, I'm new to the forum and to C++.

I'm working through some exercises in a book, and I ran into an issue when writing this code. I'm trying to figure out what is wrong with this code that causes it to go into a never ending loop when the gameName variable is assigned a string of more than one word. As long as I enter single word game titles it seems to work fine, but if I try something with 2 or more, it just starts looping through the menu forever. I can't figure out why, but I imagine it's probably obvious to someone here.

Thanks in advance!

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main()
{

	vector<string> favGames;
	vector<string>::const_iterator iter;
	string gameName;
	vector<string>::iterator existCheck;
	int choice = 0;

	cout << "\n\nWelcome to Favorite Games Manager!\n\n";

	while (choice != 4)
	{
		cout << "What would you like to do? (Enter 1-4):\n\n";
		cout << "1. List favorite games\n";
		cout << "2. Add a new favorite game\n";
		cout << "3. Remove a game.\n";
		cout << "4. Quit.\n\n";

		cin >> choice;

		switch (choice)
		{
		case 1:
			cout << "Here is a list of your games: \n";
			for (iter = favGames.begin(); iter != favGames.end(); ++iter)
			{
				cout << *iter << endl;
			}
			break;
		case 2:
			cout << "Enter the game you would like to add: \n";
			cin >> gameName;
			favGames.push_back(gameName);
			cout << gameName << " added\n";
			break;
		case 3:
			cout << "Enter the game you would like to remove: \n";
			cin >> gameName;
			existCheck = find(favGames.begin(), favGames.end(), gameName);
			if (existCheck == favGames.end())
			{
				cout << "Game not found to remove.\n";
			}
			else
			{
				favGames.erase(existCheck);
				cout << gameName << " removed\n";
			}
			break;
		case 4:
			break;
		default:
			cout << "Please enter 1-4";
			break;
		}
	}
	cout << "Thanks for using Favorite Games Manager!";

	return 0;
}
...the gameName variable is assigned a string of more than one word...


you can use a C++ function named getline . The getline function reads an entire line, including leading and embedded spaces, and stores it in a string object. The getline function looks like the following, where cin is the input stream we are reading from and gameName is the name of the string object receiving the input.

The cin.ignore function tells the cin object to skip one or more characters in the keyboard buffer.

In the below code, take a look at lines 42, 43, 50, 51.
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <limits> // To use with numeric_limits operator.

using namespace std;

int main()
{

    vector<string> favGames;
    vector<string>::const_iterator iter;
    string gameName;
    vector<string>::iterator existCheck;
    int choice = 0;

    cout << "\n\nWelcome to Favorite Games Manager!\n\n";

    while (choice != 4)
    {
	   cout << "What would you like to do? (Enter 1-4):\n\n";
	   cout << "1. List favorite games\n";
	   cout << "2. Add a new favorite game\n";
	   cout << "3. Remove a game.\n";
	   cout << "4. Quit.\n\n";

	   cin >> choice;

	   switch (choice)
	   {
	   case 1:
		  cout << "Here is a list of your games: \n";
		  for (iter = favGames.begin(); iter != favGames.end(); ++iter)
		  {
			 cout << *iter << endl;
		  }
		  break;
	   case 2:
		  cout << "Enter the game you would like to add: \n";
		  //cin >> gameName;
		  cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		  getline(cin, gameName);
		  favGames.push_back(gameName);
		  cout << gameName << " added\n";
		  break;
	   case 3:
		  cout << "Enter the game you would like to remove: \n";
		  //cin >> gameName;
		  cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		  getline(cin, gameName);
		  existCheck = find(favGames.begin(), favGames.end(), gameName);
		  if (existCheck == favGames.end())
		  {
			 cout << "Game not found to remove.\n";
		  }
		  else
		  {
			 favGames.erase(existCheck);
			 cout << gameName << " removed\n";
		  }
		  break;
	   case 4:
		  break;
	   default:
		  cout << "Please enter 1-4";
		  break;
	   }
    }
    cout << "Thanks for using Favorite Games Manager!";

    return 0;
}
Thanks for the solution! I look forward to trying it out after work.

Just out of curiosity, what gets stored in gameName by the line "cin >> gameName;" when the user types in a multi-word string that causes the looping I was experiencing? I'm just trying to understand the logic of what was going on.
Although it is possible to use cin with the >> operator to input strings, it can cause problems that you need to be aware of. When cin reads input, it passes over and ignores any leading whitespace characters (spaces, tabs, or line breaks). Once it comes to the first nonblank character and starts reading, it stops reading when it gets to the next whitespace character.

Below is to give you an idea of what value gets store (cin vs. getline):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <limits> //For the numeric_limits operation.

int main()
{
    std::string name{ " " };
    std::cout << "By not using the getline function, type the first and last name (i.e. Leroy Jenkins): ";
    std::cin >> name;
    std::cout << "You typed " << name << std::endl;

    std::cout << "By using the getline function, type the first and last name: ";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::getline(std::cin, name);
    std::cout << "You typed " << name << std::endl;

    return 0;
}


Output:

By not using the getline function, type the first and last name (i.e. Leroy Jenkins): Leroy Jenkins
You typed Leroy
By using the getline function, type the first and last name: Leroy Jenkins
You typed Leroy Jenkins
Last edited on
Thanks very much! I appreciate the help!
Topic archived. No new replies allowed.