### 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?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167`` `````` /************************ Start Program *****************************/ #include #include #include #include #include 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.