I've added only parts of the code giving the main problem. I've included useful comments for the segments giving me the trouble.
Basically the user enters a list of cities. First line (string) asks them for City's name and other unique information like State Abbreviation. 2nd line (double) allows them to enter its coordinates parallel/latitude.
When they enter a keyword like the states or province abbreviations, the program attempts to generate a list of of all the cities that share that unique information.
Header
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public:
bool foundRegion(string text) //;
{
//bool found = false;
if (name.find(text) != string::npos) //::npos is required
{
returntrue;
}
else
{
returnfalse;
}
}
for (int index = 0; index < numberOf_Cities; index++)
{
if ( (search.foundRegion(c_State_or_Province)) == true) //seems to do the opposite of what it's supposed to do.
//(!search.foundRegion(c_State_or_Province) will display "Your input did not match...." no matter what
{
cout << "Your input did not match any listing. " << endl; //Must only execute once at the most (instead of number of cities entered).
}
else {
cout << "The city " << fillArray[index].getName() <<" with parallel of ";
cout << fillArray[index].getParallel() << ", is in our catalog." <<endl;
}
}
Well that's because you are returning true if you find the instance in the string, however your code is outputting an error message if that is the case.
Well the program runs without compile errors. It just that will display data irregardless or whether the key word is in there or not, unless I rearrange the if else statement (in which case it will display "Your input did not match any listing." , even though the keyword is there).
cout << "\nEnter state/province abbreviation,";
cout << "\nto find cities that share them: ";
cin >> c_State_or_Province;
bool seen = false;
for (int index = 0; index < numberOf_Cities; index++)
{
if ( (search.foundRegion(c_State_or_Province)) == false) //(!search.foundRegion(c_State_or_Province) will display "Your input did not match...." no matter what
{
bool seen = true;
cout << "The city " << fillArray[index].getName() <<" with parallel of ";
cout << fillArray[index].getParallel() << ", is in our catalog." <<endl;
}
//else {
//cout << endl;
/// }
}
if (seen = false)
{
cout << "Your input did not match any listing. " << endl; //Must only execute once at the most (instead of number of cities entered).
}
#include <iostream>
#include <string>
#include "City.h"
usingnamespace std;
void Generate(City * cityPtr, int size);
int main()
{
City search;
string c_State_or_Province;
int numberOf_Cities;
cout << "How many cities do you want to enter: " ;
cin >> numberOf_Cities;
//Dynamically allocate an array
City * fillArray;
fillArray = new City[numberOf_Cities];
Generate(fillArray, numberOf_Cities); //User will then proceed to enter
//list of cities and their coordinates
cout << "\nEnter state/province abbreviation,";
cout << "\nto find cities that share them: ";
cin >> c_State_or_Province;
bool seen = false;
for (int index = 0; index < numberOf_Cities; index++)
{
if ( (search.foundRegion(c_State_or_Province)) == false) //(!search.foundRegion(c_State_or_Province) will display "Your input did not match...." no matter what
{
bool seen = true;
cout << "The city " << fillArray[index].getName() <<" with parallel of ";
cout << fillArray[index].getParallel() << ", is in our catalog." <<endl;
}
//else {
//cout << endl;
/// }
}
if (seen == false)
{
cout << "Your input did not match any listing. " << endl; //Must only execute once at the most (instead of number of cities entered).
}
delete [] fillArray;
fillArray = 0;
system("pause");
return 0;
}
void Generate(City * cityPtr, int size)
{
string c_Name; //Name of of city followed by useful information, like, its STATE/Province abbreviation; eg Regina SK
double c_Para; //Circle of latitude earth position; eg 50.27
for (int index = 0; index < size; index++)
{
cout << endl;
cout << "Enter name of of city followed by";
cout << "\nits STATE/Province abbreviation: ";
cin.ignore();
getline(cin, c_Name);
cityPtr[index].setName(c_Name);
cout << "Enter the parallel latitude of the city " << index + 1 << ": ";
cin >> c_Para;
cityPtr[index].setParallel(c_Para);
cout << endl;
}
}
- Why are you calling the foundRegion() on the same object every iteration? An object you don't even initialize.
- If you find a city, you assign true to a temporary variable, not the outer variable. Your last if will always fail.
- You don't need to allocate the array dynamically.
- You should pick a single style on your code. Use camel case or underscores. Add a tab before opening a new scope or not. Add a tab after opening a new scope or not. But don't use both.
for (int index = 0; index < numberOf_Cities; index++)
{
if ( (search.foundRegion(c_State_or_Province)) == false) //(!search.foundRegion(c_State_or_Province) will display "Your input did not match...." no matter what
{
seen = true;
cout << "The city " << fillArray[index].getName() <<" with parallel of ";
cout << fillArray[index].getParallel() << ", is in our catalog." <<endl;
}
else
index++;
}
if (seen == false)
{
cout << "Your input did not match any listing. " << endl; //Must only execute once at the most (instead of number of cities entered).
}
I mentioned it and now bbgst mentioned it and you don't even comment on it.
This part search.foundRegion(c_State_or_Province) doesn't have anything to do with the array element. Don't you want to check c_State_or_Province against fillArray[index].getName() or something?