Can't find the infinate loop

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//Chapter 4 Exercise
//Write a program using vectors and iterators that allows a user to maintain a list of his or her favorite books. The program should allow the user to list all book titles, add a book title, and remove a book title.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main( )
{
	vector<string> list;
	vector<string>::iterator myIterator;
	vector<string>::const_iterator iter;

	string book;

	int choice = 0;
	int position;

	cout << "A list of your favorite books\n\n";

	while( choice != 4 )
	{
		cout << "What would you like to do?\n";
		cout << "View list = 1\n";
		cout << "Add a book = 2\n";
		cout << "Remove a book = 3\n";
		cout << "Quit = 4\n\n";
		cin >> choice;
		cout << "\n";

		if( choice == 1 )
		{
			cout << "Your favorite books:\n";

			for( iter = list.begin( ); iter != list.end( ); ++iter )
			{
				cout << *iter << endl;
			}

			cout << "\n";

			continue;
		}

		if( choice == 2 )
		{
			cout << "What is the book's title?\n\n";
			cin >> book;

			list.push_back( book );

			cout << "\n";
		}

		if( choice == 3 )
		{
			for( iter = list.begin( ); iter != list.end( ); ++iter )
			{
				cout << *iter << endl;
			}

			cout << "\nWhat position is the book in?\n\n";
			cin >> position;
			cout << "\n";

			position = position - 1;

			list.erase( ( list.begin( ) + position ) );
		}

		if( choice == 4 )
		{
			break;
		}

		if( ( choice != 1 ) && ( choice != 2 ) && ( choice != 3 ) && ( choice != 4 ) )
		{
			cout << "Please enter a valid number\n\n";
		}
	}

	system( "pause" );
	return 0;
}


I made this program as an exercise for my class. It works fine until I enter a book with a name longer than one word. If the name of the book entered is longer than one word the program seems to enter an infinite loop constantly printing the choice section at the beginning of the while loop. How can I fix it?
You should read a string with getline as >> stops at the first space.

list.erase( ( list.begin( ) + position ) );
Very naughty. It works for vector, but not other containers.
I don't get what you're trying to say, sorry
You're reading the book name with:
 
cin >> book;


You should consider using:
 
getline(cin, book);
whats the difference (sorry the book I'm using doesn't talk about the getline thing)
with the getline I now hit the infinite loop even with one word titles
The major differences between:

cin >> string_variable ;

and

getline(cin, string_variable) ;

are that getline doesn't skip leading whitespace, and getline extracts the trailing '\n' from the stream.

So, if getline is used after you've used the extraction operator, the trailing whitespace (usually a '\n') is still in the stream, so that is all that getline extracts. This leaves the character data you entered for the book title still in the stream. The next thing you input is the number for choice, but the character data you entered in the stream can't be extracted for the input operation on choice as it's an int. This puts the stream in a bad state. Since you never check the state of the stream, this is never handled.

http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.2

Topic archived. No new replies allowed.