In this lab, you use what you have learned about searching an array to find an exact match to complete a partially prewritten C++ program. The program uses an array that contains valid names for 10 cities in Michigan. You ask the user to enter a city name; your program then searches the array for that city name. If it is not found, the program should print a message that informs the user the city name is not found in the list of valid cities in Michigan.

The file provided for this lab includes the input statements and the necessary variable declarations. You need to use a loop to examine all the items in the array and test for a match. You also need to set a flag if there is a match and then test the flag variable to determine if you should print the the Not a city in Michigan. message. Comments in the code tell you where to write your statements. You can use the previous Mail Order program as a guide.

Instructions
Ensure the provided code file named MichiganCities.cpp is open.
Study the prewritten code to make sure you understand it.
Write a loop statement that examines the names of cities stored in the array.
Write code that tests for a match.
Write code that, when appropriate, prints the message Not a city in Michigan..
Execute the program by clicking the Run button at the bottom of the screen. Use the following as input:
Chicago
Brooklyn
Watervliet
Acme

Answers

Answer 1

Based on your instructions, I assume the array containing the valid names for 10 cities in Michigan is named michigan_cities, and the user input for the city name is stored in a string variable named city_name.

Here's the completed program:

#include <iostream>

#include <string>

int main() {

   std::string michigan_cities[10] = {"Ann Arbor", "Detroit", "Flint", "Grand Rapids", "Kalamazoo", "Lansing", "Muskegon", "Saginaw", "Traverse City", "Warren"};

   std::string city_name;

   bool found = false;  // flag variable to indicate if a match is found

   std::cout << "Enter a city name: ";

   std::getline(std::cin, city_name);

   for (int i = 0; i < 10; i++) {

       if (city_name == michigan_cities[i]) {

           found = true;

           break;

       }

   }

   if (found) {

       std::cout << city_name << " is a city in Michigan." << std::endl;

   } else {

       std::cout << city_name << " is not a city in Michigan." << std::endl;

   }

   return 0;

}

In the loop, we compare each element of the michigan_cities array with the user input city_name using the equality operator ==. If a match is found, we set the found flag to true and break out of the loop.

After the loop, we use the flag variable to determine whether the city name was found in the array. If it was found, we print a message saying so. If it was not found, we print a message saying it's not a city in Michigan.

When the program is executed with the given input, the output should be:

Enter a city name: Chicago

Chicago is not a city in Michigan.

Enter a city name: Brooklyn

Brooklyn is not a city in Michigan.

Enter a city name: Watervliet

Watervliet is a city in Michigan.

Enter a city name: Acme

Acme is not a city in Michigan.

Read more about programs here:

https://brainly.com/question/26134656

#SPJ1


Related Questions

Other Questions
1. Paula has x cups of food in a container to feed her dogs. She pours 1. 5 cups of food into their bowls. There is now 5. 25 cups left in the container. Which equation would be used to solve this problem?a. 1. 5 - x = 5. 25b. 5. 25 - x = 1. 25c. X - 1. 5= 5. 25d. X + 1. 5= 5. 252. A box of donuts cost $9. You want to send donuts to the local nursing home. Set up an equation to find how many boxes you can send if you have $72. a. 72 = 9 + bb. 72 = 9 - bc. 72 = 9bd. 72 = 9/b3. Jordan is purchasing a board to build a bookcase. He wants to divide the board into 1. 75 foot sections and he needs 6 sections. Which equation can be used to solve this problem?a. B/1. 75 = 6b. 1. 75b = 6c. B - 1. 75 = 6d. B + 1. 75 = 6 PLSSS HELP AND PLEASE SHOW WORK ASWELL Collin has 100 feet of fencing to enclose a pen for his puppy. He istrying to decide whether to make the pencircular or square. He plans to use all of thefencing. Part A.) If Collin uses all of the fencing, whatwould be the area of each pen? Use 3.14for pie. Round to the nearest hundredth ifnecessary. Part B.) To have the largest possible area for the pen, which pen should Collin build? two CSR aspects (with specific details / examples) about JackMAtwo influence tactics (with specific details / examples) aboutJack MA see attachment3(3m+N) 17 Read the text and write 5 questions you would like to ask Andrew Stock has three pets: a hamster, a budgie and a dog. His hamster Jeremy is serious and always busy. Jeremy has his own small house. Sunny, the budgie, is sociable and talkativ The third pet is Max, a big black dog. Max likes playing with his ball. Andrew likes his pets very much. 3. Peter owns a small grain elevator. He hires three helpers at $35,000 per year for eachworker, pays annual rent of $7,000 for his shop, and spends $60,000 per year on materials. Hehas invested some of his own funds in equipment (augers, bins, etc) that could earn him $3,000per year if alternatively invested. He has been offered $65,000 per year to work as a managerfor a competitor. He estimates his entrepreneurial talents are worth $5,000 per year. Totalannual revenue from the grain elevator is $235,000. Calculate accounting profit and economicprofit for Peter's elevator. Show your work. Label all associated costs as implicit or explicit. Graph the line that represents a proportional relationship between ddd and ttt with the property that an increase of 333 units in ttt corresponds to an increase of 444 units in ddd.What is the unit rate of change of ddd with respect to ttt? (That is, a change of 111 unit in ttt will correspond to a change of how many units in ddd?)The unit rate is .Graph the relationship. PLEASE HELPConstruct the indicated confidence interval for the population mean u using the t-distribution. Assume the population is normally distributed.C=0.90, x=13.7, s =3.0, n= 10 With which claim would the author of ""A Mountain Calling"" agree? A Private nature parks should be turned over to the U. S. Forest Service. B Rushing to see as much of a natural park as possible is sometimes necessary. C America finally has enough national parks to satisfy everyones needs. D Every American should try to visit a national park at least once in his or her life How are democratic ideals reflected in the declaration of independence. The times to run a mile are 7,9, 10, 11, 11, and 12.What is the mean? A teacher uses a stream table to model how a river changes the surface of earth. when the teacher finishes pouring the water, which change will most likely have taken place? Gas prices are on the rise this summer as more Americans travel, Prices have increased by 40% from last summer's price, which averaged $2. 26 per gallon. Jonah's mom's car takes 14 1/2 gallons of gas to fill up. Find the cost that Jonah's mom would have to spend after the 40% increase in gas prices if she fills up her tank the entire 14 1/2 gallons. please i need answer to this Asap. Only question A, B, C, E . With solution Asap vSteve has realized that he accidentally plagiarized source material in sentence 8. What is the best way to credit the source?Answer choices for the above questionBy quoting it directly and referencing the source in the sentenceBy removing any direct or indirect reference to the idea in the quotationBy paraphrasing the quotation and putting the paraphrased text in quotesBy providing a bibliographical citation directly after the sentence A garden bed is 4 by 3 and a 6 layer of soil will be spread over the garden. A bag of soil contains 2ft3 of soil how many bags r needed Michael had 8/9 of a spool of yarn. He used 2/5 of his yarn for a project. What fraction of the spool was used for the project? HELP ME UNDERSTAND THIS Parametric Equations QuestionA drone traveling horizontally at 100 m/s over flat ground at an elevation of 4500 meters must drop an emergency package on a target on the ground. The trajectory of the package is given by [tex]x=100t, y=-4.9t^2 +4500, t\geq 0[/tex]where the origin is the point on the ground directly beneath the drone at the moment of release. How many horizontal meters before the target should the package be released in order to hit the target? Round to the nearest meter. Think about a time when you use your creativity to write a paper or class project ? What was enjoyable about the experience ? What do you dislike about the experience