Having trouble passing an int between functions

My CS teacher is pretty picky so I have to call a function from another function,if you notice in my findCityData function I'm supposed to be calling my searchFunction function so it can perform the binary search and return the subscript that I have named "position". I'm having trouble getting it to do all of this so if someone could look at my code and give me some suggestions that would be great.

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244





#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

//Function prototypes
void fillArray (string city[], double budget[], double revenue[], int &NumCities);
void printarrays (string city[], double budget[], double revenue[], int NumCities);
void FindHiBudget (string city[], double budget[], int NumCities);
void AboveAvgRevenue (string city[], double revenue[], int NumCities);
void OverBudget (string city[], double budget[], double revenue[], int NumCities);
int searchFunction (string city[], int NumCities, string target);
void findCityData (string city[], double budget[], double revenue[], int NumCities);
//ADD YOUR PROTOTYPES HERE

const int ArraySize = 30;

int main ()
{
	string city[ArraySize];
	double budget[ArraySize];
	double revenue[ArraySize];
	int NumCities;
	

	//We will now call on all of these functions
	fillArray (city, budget, revenue, NumCities);
	cout << left << fixed << setprecision(0);
	printarrays (city, budget, revenue, NumCities);

   //ADD THE CALL TO YOUR FindHiBudget function
   FindHiBudget (city, budget, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR AboveAvgRevenue function
	AboveAvgRevenue (city, revenue, NumCities);
    cout << "======================================================" << endl;

    
    //ADD THE CALL TO YOUR OverBudget function
	OverBudget (city, budget, revenue, NumCities);
    cout << "======================================================" << endl;
	
	
	
	findCityData (city, budget, revenue, NumCities)
	cout << "======================================================" << endl;



	return 0;

}

void fillArray (string city[], double budget[], double revenue[], int &NumCities)
{
	ifstream indata;
	NumCities = 0;

	//Now we will open the file

	indata.open("lab1.txt");
	if (! indata)
	{
		cerr << "Error" << endl;
		
	}


	getline(indata, city[NumCities]);

	//We will now run a loop to get al of the data from the file and assign it to the proper array
	while (!indata.eof()&& NumCities < ArraySize)
	{	
		indata >> budget[NumCities];
		indata >> revenue[NumCities];

		indata.ignore();
        NumCities++;
		getline(indata, city[NumCities]);
		
	}
	return;
}

void printarrays (string city[], double budget[], double revenue[], int NumCities)
{

	cout << setw(20) << "City" << setw(20) << "Revenue" << setw(20) << "Budget" << endl;
	cout << setw(20) << "----" << setw(20) << "-------" << setw(20) << "------" << endl;
	

	//We will now print al of the data in a table
	for (int i = 0; i < NumCities; i++)
	{
		cout << setw(20) << city[i] << setw(20) << revenue[i] << setw(20) << budget[i] << endl;
	}
}

//========================================================
// ADD YOUR FindHiBudget function here
//========================================================
void FindHiBudget (string city[], double budget[], int NumCities)
{
	int i = 0;
	int HiBudget = 0;
	int temp = 0;
	
for (i = 0; i < NumCities; i++)
{
	if (budget[i] > HiBudget)
	{
		HiBudget = budget[i];
		temp = i;
	}
	
	
}
cout << "======================================================" << endl;
cout << "The city with the highest budget is " << city[temp] << endl;
}



//========================================================
// ADD YOUR AboveAvgRevenue function here
//========================================================
void AboveAvgRevenue (string city[], double revenue[], int NumCities)
{
	double average = 0;
	int sum = 0;
	int i = 0;
	
	for (i = 0; i < NumCities; i++)
	{
		sum += revenue[i];
	}
	
	average = sum / NumCities;
	cout << "The average revenue is " << average << endl;
	cout << "The cities with the above average revenue are: " << endl;
	
	for (i = 0; i < NumCities; i++)
	{
		if (revenue[i] > average)
			cout << city[i] << endl;
	}
}
	






//========================================================
// ADD YOUR OverBudget function here
//========================================================
 void OverBudget (string city[], double budget[], double revenue[], int NumCities)
{
	int i = 0;
	
	cout << "The cities that are over budget are: " << endl;
	
	for (i = 0; i < NumCities; i++)
	{
		if (budget[i] > revenue[i])
			cout << city[i] << " by " << budget[i] - revenue[i] << endl;
	}
}
	
	

void findCityData (string city[], double budget[], double revenue[], int NumCities)
{ 
	string target;
	cout << "Enter the name of the city or Q to quit:";
	cin >> target;
	while (target !=-1)
	{
		searchFunction (city, NumCities, target);
		cout << city[position] << " has a budget of " << budget[position] << " and revenue totaling " << revenue[position] << endl;
		cout << "Enter the name of the city or Q to quit:";

	}




}










	
	
int searchFunction (string city[], int NumCities, string target)
{

	
	int first = 0,           // First array element
       last = NumCities - 1, // Last array element
       middle,              // Mid point of search
       position = -1;       // Position of search value
   bool found = false;      // Flag

   while (!found && first <= last)
   {
      middle = (first + last) / 2;  // Calculate mid point
      if (city[middle] == target)   // If value is found at mid
      {
         found = true;
         position = middle;
      }
      else if (city[middle] > target) // If value is in lower half
         last = middle - 1;
      else
         first = middle + 1;          // If value is in upper half
   }

   cout << position << endl;
   return position;
}









Here's what the output is SUPPOSED to look like. ( I had to shorten it because it was too long to post).
City                Revenue             Budget
----                -------             ------
Baltic              38000200            35000000
Baltimore           89000300            58500000
Bexley              28481063            25478963
Bloomdale           38410275            58412475
Blue Ash            35898625            25896325
.......
======================================================
The city with the highest budget is Bradford
======================================================
The average revenue is 59148500
The cities with above average revenue are:
Baltimore
Bluffton
....
======================================================
The cities over budget are:
Bloomdale by 20002200
Bolivar by 16197500
Bowling Green by 7997200
.....
======================================================
Enter the name of the city or Q to quit:Baltimore
Baltimore has a budget of 58500000 and revenue totaling 89000300
Enter the name of the city or Q to quit:r
r not found.
Enter the name of the city or Q to quit:Q
===============After Sorting By Budget================
City                Revenue             Budget
----                -------             ------
Bratenahl           14460032            21456932
Brookville          27569478            23565478
.............
==============After Sorting By Revenue ===============
City                Revenue             Budget
----                -------             ------
Bratenahl           14460032            21456932
Bradner             19690445            25687445
................
Last edited on
Your function named searchFunction returns an int.

If you want to use that int, you will have to assign it to something.

For example,

1
2
int someValue;
someValue = searchFunction (city, NumCities, target);


I think I understand but I'm not sure what steps to take.

This,
1
2
int someValue;
someValue = searchFunction (city, NumCities, target);

Doesn't look like anything I've seen before so I don't fully understand it.
I get it now....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void findCityData (string city[], double budget[], double revenue[], int NumCities)
{ 
	string target;
	int pos;
	cout << "Enter the name of the city or Q to quit:";
	getline (cin,target);
	while(target.size() > 0)
	{
		pos = searchFunction (city, NumCities, target);
		cout << city[pos] << " has a budget of " << budget[pos] << " and revenue totaling " << revenue[pos] << endl;
		cout << "Enter the name of the city or Q to quit:";
		getline (cin,target);

	}





}


Now, just need to find a way to make Q end the loop.
Topic archived. No new replies allowed.