Bool member function won't return argument correctly

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
				{
				return true;
				}
			else
				{
				return false;
				}
			}


cout << "\nEnter state/province abbreviation,";
cout << "\nto find cities that share them: ";

cin >> c_State_or_Province;

Int main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	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).
firedraco was correct, he just explained the problem in a confusing way.

You have it set to return true if it DOES exist, however your if statement there prints the error message when it returns true.
Yeah I understand now. But that't not the problem. The program displays all the cities even when some of them don't have the keyword...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
	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).
		}
search and c_State_or_Province seems to be independent of the index so foundRegion will return the same value for all iterations.

seen = false should be seen == false on line 26
Last edited on
Come on man, help help help :'(

Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef CITY_H 
#define CITY_H
#include <string>
using namespace std;

class City
{ 
private: 
		string name; //Name of of city followed by useful information, like, its STATE/Province abbreviation; eg Regina SK
		double parallel;  //Circle of latitude earth position; eg 50.27	

	
public: 
		City()  //;		//constructor
		{	name = "No city";
			parallel = 0.00;	}

		void  setName(string cityName)
		    {name = cityName;}


		void  setParallel(double cityParallel) // ;
			{parallel = cityParallel;} 

		string getName()   //; 
			{return name;} 

		double  getParallel()  const //;
			{return parallel;} 

		bool foundRegion(string text) //;
			{
			//bool found = false;
				
			if (name.find(text) != string::npos)  //::npos is required
				{
				return true;
				}
			else
				{
				return false;
				}
			}
};
#endif  


Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include  <iostream> 
#include  <string> 
#include  "City.h" 
using  namespace  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;		
	}
}
seen on line 38 is not the same as seen on line 31
Aah cool, that atleast settles 33% of my problems :D. Thank you very much, sir!

But still all the cities that aren't supposed to appear... appear :(
- 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.
dynamic array is required. By the way i'm not good with terminologies.

seen = true keeps executing though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	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).
		}
Last edited on
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?
Hmm i think i may see where you're coming from. Unfortunately implementing such syntax has me confused.

instead of if ( (search.foundRegion(c_State_or_Province)) == false)

you're saying

if ( (fillArray[index].getName(c_State_or_Province) == false)

which creates a syntax error >:'(
I didn't say anything about syntax. I just pointed out what I think can be a mistake. How you solve this is up to you.
Topic archived. No new replies allowed.