String Code using a sentinel value

I have to write a program that allows me to enter three separate strings: a city name, a state name, ad a Zip code. the program should use string concatenation to display the city name followed by a comma, a space, the state name, two spaces, and the Zip code. Use a sentinel value to end the program.

I have the code figured out except the part about the sentinel value. I am hoping someone can point me in the right direction. The following is my code so far:


#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;


int main()
{
string city = "";
string state = "";
string zipCode = "";
string combined = "";



cout << "Enter city: ";
getline(cin, city);
cout << "Enter state: ";
getline(cin, state);
cout << "Enter zip code: ";
getline(cin, zipCode);
combined = city + ", " + state + " " + zipCode;
cout << combined << endl << endl;



system("PAUSE");
return 0;
}
Blank string as sentinel?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string city, state, zipCode;
   while ( true )
   {
      cout << "Enter city (blank to finish): ";   getline( cin, city );
      if ( city == "" ) return 0;
      cout << "Enter state: ";                    getline( cin, state );
      cout << "Enter zip code: ";                 getline( cin, zipCode );
      cout << city + ", " + state + "  " + zipCode << '\n';
   }
}
Yes! Runs perfect now. Thank you so much!
Topic archived. No new replies allowed.