Errors with passing arrays into functions

Hi all,

I'm having some errors with trying to pass into functions. How do I address these problems below?
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

/************************ Start Program *****************************/

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

using namespace std;

bool openAndVerify(ifstream &, string &, int);
double averageCalc(ifstream &);
double stndrdDevCalc(ifstream &, string &, double, int);
void print(double, double, string, int);
string getBankName(ifstream &);

// Part E

int main() {

	int counter, numDesired, numCount;
	double mean[10]; 
	double stndrdDev[10];
	string bankName;
	string fileName[10];
	ifstream inputFile;
	double min = 0;
	string betterBank;
	cout << "Greetings!\nEnter number of banks would you like to analyze: ";
	cin >> numDesired;
	counter = 0;

	while(counter < numDesired) { 

		if(openAndVerify(inputFile, fileName[10], counter) == false) {
			cout << "Error: bad file name. Program terminated!" << endl;
			return 0;
		} else {		
			bankName = getBankName(inputFile);
			cout << bankName << endl;
			mean[counter] = averageCalc(inputFile);
	
			stndrdDev[counter] = stndrdDevCalc(inputFile, fileName[counter], mean);

			if (counter == 0) {
			
				betterBank = bankName;
				min = stndrdDev[0];

			}

			if (stndrdDev[counter] < min){

				min = stndrdDev[counter];
				betterBank = bankName;		

			}		
		
			print(stndrdDev[counter], mean[counter], bankName);
		
			counter++;
		}

	}
	
	cout << betterBank << " is the preferred bank because it has the lowest standard deviation." << endl;

	return 0;

}


// PART A

bool openAndVerify(ifstream &inputFile, string[] &fileName, int counter) {

	cout << "Please enter file name: ";
	//getline(inputFile, fileName);
	//getline(cin, fileName);

	cin >> fileName[counter];
	cout << "Filename: " << fileName[counter] << endl;
	inputFile.open(fileName[counter].c_str());
	if(inputFile.fail()) {

		inputFile.close();
		return false; 

	} else {

		return true; 

	}

}


// PART B

double averageCalc(ifstream &inputFile) {

	int numEntries = 0;
	double sum;
	double time[20];
	sum = 0;

	while(!inputFile.eof()) {

		inputFile >> time[numEntries];
		sum += time[numEntries];
		numEntries++;

	}

	inputFile.close();
	return sum / numEntries;

}


// PART C

double stndrdDevCalc(ifstream &inputFile, string[] &fileName, double[] mean, int counter) {

	int numEntries = 0;
	double time, stndrdDevCalcNumerator = 0;
	
	inputFile.open(fileName[counter].c_str());
	inputFile.ignore(500, '\n');
	
	while(!inputFile.eof()) {

		inputFile >> time;
		stndrdDevCalcNumerator += pow((time - mean), 2);
		numEntries++;
	
	}

	inputFile.close();
	return sqrt(stndrdDevCalcNumerator / (double(numEntries) - 1));
	

}

// Part D

void print(double[] stndrdDev, double[] mean, string bankName, int counter) {

	cout << "===============================================" << endl;
	cout << "Bank Name: " << bankName << endl;
	cout << setprecision(3) << showpoint << fixed;
	cout << "Average wait time: " << mean[counter] << endl;
	cout << "Standard Deviation of the wait time list: " << stndrdDev[counter] << endl;
	cout << "===============================================" << endl << endl;

}

string getBankName(ifstream &inputFile) {

	string bankName;
	getline(inputFile, bankName);
	return bankName;
	
}	




aludra.usc.edu(8): assignment5.cpp: In function 'int main()':
assignment5.cpp:139: error: cannot convert 'double*' to 'double' for argument '3' to 'double stndrdDevCalc(std::ifstream&, std::string&, double, int)'
assignment5.cpp:110: error: too few arguments to function 'void print(double, double, std::string, int)'
assignment5.cpp:155: error: at this point in file
assignment5.cpp: At global scope:
assignment5.cpp:171: error: expected ',' or '...' before '&' token
assignment5.cpp: In function 'bool openAndVerify(std::ifstream&, std::string*)':
assignment5.cpp:177: error: 'fileName' was not declared in this scope
assignment5.cpp:177: error: 'counter' was not declared in this scope
assignment5.cpp: At global scope:
assignment5.cpp:219: error: expected ',' or '...' before '&' token
assignment5.cpp: In function 'double stndrdDevCalc(std::ifstream&, std::string*)':
assignment5.cpp:224: error: 'fileName' was not declared in this scope
assignment5.cpp:224: error: 'counter' was not declared in this scope
assignment5.cpp:230: error: 'mean' was not declared in this scope
assignment5.cpp: At global scope:
assignment5.cpp:243: error: expected ',' or '...' before 'stndrdDev'
assignment5.cpp: In function 'void print(double*)':
assignment5.cpp:246: error: 'bankName' was not declared in this scope
assignment5.cpp:248: error: 'mean' was not declared in this scope
assignment5.cpp:248: error: 'counter' was not declared in this scope
assignment5.cpp:249: error: 'stndrdDev' was not declared in this scope

I don't have much time, but the compiler tells you everything that you need.

First, change the declaration/prototype to:
double stndrdDevCalc(ifstream &, string &, double[], int);
and the definition to:
double stndrdDevCalc(ifstream &inputFile, string fileName[], double mean[], int counter) {

Next change the way you call it to:
stndrdDev[counter] = stndrdDevCalc(inputFile, fileName[counter], mean, counter);

Stick the [] AFTER the name of the object in your definitions
Also I've noticed that in some cases (such as openAndVerify() ) you send just one element from the array (example filename[10]). When you do this, just treat the input in the function as a non-array. So wait for a string, not a string[].
Topic archived. No new replies allowed.