Program Menu choice skips over input

Hello everyone, I just recently finished up a program that implements Dijkstra's Algorithm and I thought I would make a nice little menu for the user.

so the user can input their locations, I use Getline just in case their location contains spaces. However, when the user selects 2, It skips over the first input for the "Source" location and only lets the user input for the "Destination".

Any reason why this would happen? Am I missing something with Getline?

Anyway, here's the code...

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
template <typename V, typename E>
void Graph<V, E>::Driver()
{
	bool done = false;
	int menu(0);

	InputFile();

	while(done == false)
	{
		system("CLS");
		cout << "---Main Menu---"
			 << "\n1. View Locations"
			 << "\n2. Input Locations"
			 << "\n3. Perform Dijkstras"
			 << "\n4. Quit";
		cout << "\nChoice: ";
		cin >> menu;

		if(menu == 1)
		{
			//display all vertices in Graph
			cout << endl << endl;
			DepthFirst();
			cout << "\nPress enter to go back" << endl;
			system("Pause");
		}
		else if(menu == 2)
		{
			cout << "\nStarting Location: ";//problem here!
			getline(cin, m_Src);
			cout << "\nEnding Location: "; 
			getline(cin, m_Dst);     //skips to this input
		}
		else if(menu == 3)
		{
			DijkstrasAlgorithm();
			ShowPath();
		}
		else if(menu == 4)
		{
			done = true;
		}
	}
}
statements like this cin >> menu; should be followed by cin.ignore(); to deal with the newline left in the input stream from the user pressing enter.


Side note: this while(done == false) should be while(done) the first version is error prone for example people will do this by accident while(done = false) which is an assignment statement and will allways be false.
Last edited on
That fixed it. Thanks a bunch Yanson!
Topic archived. No new replies allowed.