Sentinel Value

How would one use a sentinel value to stop this application. I obviously can't leave it as is because the cityName is outside the while loop, I just can't grasp how to use a sentinel value in this situation or why its even needed here...
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
  #include <iostream>
#include <string>
using namespace std;

int main()
{
	string cityName = "";
	string stateName = "";
	string zipCode = "";
	string info = "";



	cout << "City (-1 to end): ";
	getline(cin, cityName);

	while (cityName != "-1")
	{
		cout << "State: ";
		getline(cin, stateName);

		cout << "Zip code: ";
		getline(cin, zipCode);

		info = cityName + ", " + stateName + "  " + zipCode;

		cout << info << endl;
	
	}//end of while


	system("pause");
	return 0;
} // end of main 
Last edited on
I added
cout << "City (-1 to end): ";
getline(cin, cityName);
to the end of my while loop to fix the issue but is this the optimal way to do so?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while (cityName != "-1")
	{
		cout << "State: ";
		getline(cin, stateName);

		cout << "Zip code: ";
		getline(cin, zipCode);

		info = cityName + ", " + stateName + "  " + zipCode;

		cout << info << endl;

		cout << "City: ";
		getline(cin, cityName);
	
	}//end of while 
Last edited on
****Bueller****
Topic archived. No new replies allowed.