Answer:
// MichiganCities.cpp - This program prints a message for invalid cities in Michigan.
// Input: Interactive
// Output: Error message or nothing
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Declare variables
string inCity; // name of city to look up in array
const int NUM_CITIES = 10;
// Initialized array of cities
string citiesInMichigan[] = {"Acme", "Albion", "Detroit", "Watervliet", "Coloma", "Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"};
bool foundIt = false; // Flag variable
int x; // Loop control variable
// Get user input
cout << "Enter name of city: ";
cin >> inCity;
// Write your loop here
for (x = 0; x < NUM_CITIES; x++) {
// Write your test statement here to see if there is
// a match. Set the flag to true if city is found.
if (citiesInMichigan[x] == inCity) {
foundIt = true;
break;
}
}
if (!foundIt)
cout << "Not a city in Michigan." << endl;
else
cout << "Valid city in Michigan." << endl;
return 0;
} // End of main()
Step-by-step explanation:
In this version of the code, we added a loop that goes through the citiesInMichigan array and compares each element with the input city using an if statement. If a match is found, the foundIt flag is set to true and the loop is exited using break. Finally, we added an else block to the conditional statement that prints "Valid city in Michigan." if the city is found.