Help with basic array

Problem is to search array for exact match. I have my code and it appears to work, the only problem is my output prints 11 times and for the life of me I can't figure out why....

// 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++)
{
if (inCity == citiesInMichigan[x])
{
foundIt = true;
x+=1;
}


// Write your test statement here to see if there is
// a match. Set the flag to true if city is found.
if(foundIt == true){
cout << inCity << " is in Michigan" << endl;
}
else
{
cout << "Not a city in Michigan" << endl;
}


}
// Test to see if city was not found to determine if
// "Not a city in Michigan" message should be printed.


return 0;
}
@weyekin03

for(x=0; x<=NUM_CITIES; x++)

Because of this line. NUM_CITIES is assigned to 10, and this loop goes from 0 to, and including, 10, which makes 11. If you want ONLY 10, replace <= with just <.

And, remove the x+=1; after the foundIt = true line. It really isn't needed.
Last edited on
Thank you for that pointer but I guess I was not clear enough in my question. I would believe when I enter a city name that it should run through the loop and determine if that city is part of the array. For example if I enter Chicago and that is not in my array. It would print "Not a city in Michigan" one time and not "Not a city in Michigan" 10 times. I am sure I must initialize the array as I did......
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for(x=0; x< NUM_CITIES; x++)
{
      if (inCity == citiesInMichigan[x])
      {
            foundIt = true;
      }
      // Write your test statement here to see if there is
      // a match. Set the flag to true if city is found.
      if(foundIt == true)
      {
            cout << inCity << " is in Michigan" << endl;
      }
      else
      {
            cout << "Not a city in Michigan" << endl;
      }
}


Everything that is caught in a for's {. . .} will be looped for a number of times before the loops end (for example : 10 times)

Solution :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for(x=0; x< NUM_CITIES; x++)
{
      if (inCity == citiesInMichigan[x])
      {
            foundIt = true;
      }
      // Write your test statement here to see if there is
      // a match. Set the flag to true if city is found.
}

if(foundIt == true)
{
      cout << inCity << " is in Michigan" << endl;
}
else
{
      cout << "Not a city in Michigan" << endl;
}
Thanks for the comment...I have submitted not happy with results. Going back to the books to try and understand more clearly.
Topic archived. No new replies allowed.