### Calculating Mean & Standard Deviation using functions

Hey guys,

This is my first post. I'm posting because I have an assignemnt due in 3 hours and don't know what's wrong with my code.

My code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150`` ``````#include #include #include #include #include using namespace std; bool openAndVerify(ifstream &, string &); double averageCalc(ifstream &, string &); double stndrdDevCalc(ifstream &, string &, double); void print(double, double, string); string getBankName(ifstream &, string &); int main() { int counter, numDesired; cout << "Greetings!\nEnter number of banks would you like to analyze: "; cin >> numDesired; counter = 0; while(counter < numDesired) { double mean, stndrdDev; string bankName, fileName; ifstream inputFile; openAndVerify(inputFile, fileName); if(!openAndVerify(inputFile, fileName)) { do { openAndVerify(inputFile, fileName); } while (!openAndVerify(inputFile, fileName)); } bankName = getBankName(inputFile, fileName); mean = averageCalc(inputFile, fileName); stndrdDev = stndrdDevCalc(inputFile, fileName, mean); print(stndrdDev, mean, bankName); counter++; } return 0; } // PART A bool openAndVerify(ifstream inputFile, string &fileName) { cout << "Please enter file name: "; cin >> fileName; inputFile.open(fileName.c_str()); if(inputFile.fail()) { cout << "Error. Bad file name. Try again."; return false; } else { return true; } inputFile.close(); } // PART B double averageCalc(ifstream &inputFile, string &fileName) { int numEntries = 0; double time, sum; sum = 0; inputFile.open(fileName.c_str()); inputFile.ignore(500, '\n'); //getline(inputFile, fileName) while(!inputFile.eof()) { inputFile >> time; sum += time; numEntries++; } inputFile.close(); return sum / numEntries; } // PART C double stndrdDevCalc(ifstream inputFile, string fileName, double mean) { int numEntries = 0; double time, stndrdDevCalcNumerator = 0; inputFile.open(fileName.c_str()); inputFile.ignore(500, '\n'); while(!inputFile.eof()) { inputFile >> time; stndrdDevCalcNumerator += pow((time - mean), 2); numEntries++; } inputFile.close(); return sqrt(stndrdDevCalcNumerator / (numEntries - 1)); } // Part D void print(double stndrdDev, double mean, string bankName) { cout << "===============================================" << endl; cout << "Bank Name: " << bankName << endl; cout << setprecision(1) << showpoint << fixed; cout << "Average wait time: " << mean << endl; cout << "Standard Deviation of the wait time list: " << stndrdDev << endl; cout << "===============================================" << endl << endl; } string getBankName(ifstream inputFile, string fileName) { string bankName; inputFile.open(fileName.c_str()); inputFile >> bankName; return bankName; } ``````

The errors:

 assn4.cpp: In function 'bool openAndVerify(std::ifstream, std::string&)': assn4.cpp:61: error: no matching function for call to 'std::basic_ifstream >::open(std::basic_string, std::allocator >&)' /auto/usc/gnu/gcc/4.2.1/bin/../lib/gcc/sparc-sun-solaris2.10/4.2.1/../../../../include/c++/4.2.1/fstream:517: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits] assn4.cpp: In function 'double averageCalc(std::ifstream&, std::string&)': assn4.cpp:86: error: no matching function for call to 'std::basic_ifstream >::open(std::basic_string, std::allocator >&)' /auto/usc/gnu/gcc/4.2.1/bin/../lib/gcc/sparc-sun-solaris2.10/4.2.1/../../../../include/c++/4.2.1/fstream:517: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits] assn4.cpp: In function 'double stndrdDevCalc(std::ifstream, std::string, double)': assn4.cpp:112: error: no matching function for call to 'std::basic_ifstream >::open(std::string&)' /auto/usc/gnu/gcc/4.2.1/bin/../lib/gcc/sparc-sun-solaris2.10/4.2.1/../../../../include/c++/4.2.1/fstream:517: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits] assn4.cpp: In function 'std::string getBankName(std::ifstream, std::string)': assn4.cpp:146: error: no matching function for call to 'std::basic_ifstream >::open(std::string&)' /auto/usc/gnu/gcc/4.2.1/bin/../lib/gcc/sparc-sun-solaris2.10/4.2.1/../../../../include/c++/4.2.1/fstream:517: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits]
Last edited on
The error relates to the fact that you used a string instead a cstring (char*) as the parameter to the open() call. To fix the problem, use open(fileName.c_str());
Last edited on
I notice that all of the errors are inside functions and on the line that has:
`inputFile.open(fileName);`
What's wrong with that line of code?
Thanks Gulshan Singh!

I've edited the original posted code to include the fix
I did that and now I have a new errors:

 ```Undefined first referenced symbol in file getBankName(std::basic_ifstream >&, std::basic_string, std::allocator >&)/var/tmp//ccDFTpxe.o stndrdDevCalc(std::basic_ifstream >&, std::basic_string, std::allocator >&, double)/var/tmp//ccDFTpxe.o openAndVerify(std::basic_ifstream >&, std::basic_string, std::allocator >&)/var/tmp//ccDFTpxe.o ld: fatal: Symbol referencing errors. No output written to a.out collect2: ld returned 1 exit status ```
The function prototypes have variables declared as pass by reference:
string getBankName(ifstream &, string &);

But the function definitions have pass by value:

string getBankName(ifstream inputFile, string fileName) {

string bankName;
inputFile.open(fileName.c_str());
inputFile >> bankName;
return bankName;

}
THANK YOU SO MUCH :D :D :D
I've been coding since 12am last night
slept from 10am - 2pm
got up started coding
and now (8:55pm) is the first time i haven't had an errors
Lol, that's the life of a CS student. Good luck on your project.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153`` ``````#include #include #include #include #include using namespace std; bool openAndVerify(ifstream &, string &); double averageCalc(ifstream &, string &); double stndrdDevCalc(ifstream &, string &, double); void print(double, double, string); string getBankName(ifstream &, string &); int main() { int counter, numDesired; cout << "Greetings!\nEnter number of banks would you like to analyze: "; cin >> numDesired; counter = 0; while(counter < numDesired) { double mean, stndrdDev; string bankName, fileName; ifstream inputFile; openAndVerify(inputFile, fileName); if(openAndVerify(inputFile, fileName) == false) { do { openAndVerify(inputFile, fileName); } while (openAndVerify(inputFile, fileName) == false); } bankName = getBankName(inputFile, fileName); mean = averageCalc(inputFile, fileName); stndrdDev = stndrdDevCalc(inputFile, fileName, mean); print(stndrdDev, mean, bankName); counter++; } return 0; } // PART A bool openAndVerify(ifstream &inputFile, string &fileName) { cout << "Please enter file name: "; cin >> fileName; inputFile.open(fileName.c_str()); if(inputFile.fail()) { cout << "Error. Bad file name. Try again." << endl; return false; } else { return true; } inputFile.close(); } // PART B double averageCalc(ifstream &inputFile, string &fileName) { int numEntries = 0; double time, sum; sum = 0; inputFile.open(fileName.c_str()); inputFile.ignore(500, '\n'); //getline(inputFile, fileName) while(!inputFile.eof()) { inputFile >> time; sum += time; numEntries++; } inputFile.close(); return sum / numEntries; } // PART C double stndrdDevCalc(ifstream &inputFile, string &fileName, double mean) { int numEntries = 0; double time, stndrdDevCalcNumerator = 0; inputFile.open(fileName.c_str()); inputFile.ignore(500, '\n'); while(!inputFile.eof()) { inputFile >> time; stndrdDevCalcNumerator += pow((time - mean), 2); numEntries++; } inputFile.close(); return sqrt(stndrdDevCalcNumerator / (numEntries - 1)); } // Part D void print(double stndrdDev, double mean, string bankName) { cout << "===============================================" << endl; cout << "Bank Name: " << bankName << endl; cout << setprecision(1) << showpoint << fixed; cout << "Average wait time: " << mean << endl; cout << "Standard Deviation of the wait time list: " << stndrdDev << endl; cout << "===============================================" << endl << endl; } string getBankName(ifstream &inputFile, string &fileName) { string bankName; inputFile.open(fileName.c_str()); inputFile >> bankName; return bankName; } ``````

 ``` Greetings! Enter number of banks would you like to analyze: 2 Please enter file name: alpha_bank.txt Please enter file name: alpha_bank.txt Error. Bad file name. Try again. Please enter file name: ```

How come there is an infinite loop?
Last edited on
 ``1234567`` ``````if(!openAndVerify(inputFile, fileName)) { do { openAndVerify(inputFile, fileName); } while (!openAndVerify(inputFile, fileName)); }``````

If open openAndVerify() fails, it tries again. But in your openAndVerify() function do you ever try to fix the problem that caused it to fail? It looks like you simply return false again.
I thought that perhaps it was:
 ``1234567`` ``````if(openAndVerify(inputFile, fileName) == false) { do { openAndVerify(inputFile, fileName); } while (openAndVerify(inputFile, fileName) == false); }``````

but when I coded that out my output was:

 ``` Greetings! Enter number of banks would you like to analyze: 2 Please enter file name: alpha_bank.txt ```

Basically, after I pressed enter to input the file name, nothing happened and I could keep on pressing enter forever and had to ctrl c out of it
lol we posted at the same time

I'll take that into account and see what I can do

again, thanks for helping me out with all this ^^
Current Code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147`` ``````#include #include #include #include #include using namespace std; bool openAndVerify(ifstream &, string &); double averageCalc(ifstream &, string &); double stndrdDevCalc(ifstream &, string &, double); void print(double, double, string); string getBankName(ifstream &, string &); int main() { int counter, numDesired; cout << "Greetings!\nEnter number of banks would you like to analyze: "; cin >> numDesired; counter = 0; while(counter < numDesired) { double mean, stndrdDev; string bankName, fileName; ifstream inputFile; if(openAndVerify(inputFile,fileName) == false) { cout << "Error: bad file name. Program terminated!" << endl; return 0; } bankName = getBankName(inputFile, fileName); cout << bankName; mean = averageCalc(inputFile, fileName); stndrdDev = stndrdDevCalc(inputFile, fileName, mean); print(stndrdDev, mean, bankName); counter++; } return 0; } // PART A bool openAndVerify(ifstream &inputFile, string &fileName) { cout << "Please enter file name: "; cin >> fileName; inputFile.open(fileName.c_str()); if(inputFile.fail()) { inputFile.close(); return false; } else { inputFile.close(); return true; } } // PART B double averageCalc(ifstream &inputFile, string &fileName) { int numEntries = 0; double time, sum; sum = 0; inputFile.open(fileName.c_str()); inputFile.ignore(500, '\n'); //getline(inputFile, fileName) while(!inputFile.eof()) { inputFile >> time; sum += time; numEntries++; } inputFile.close(); return sum / numEntries; } // PART C double stndrdDevCalc(ifstream &inputFile, string &fileName, double mean) { int numEntries = 0; double time, stndrdDevCalcNumerator = 0; inputFile.open(fileName.c_str()); inputFile.ignore(500, '\n'); while(!inputFile.eof()) { inputFile >> time; stndrdDevCalcNumerator += pow((time - mean), 2); numEntries++; } inputFile.close(); return sqrt(stndrdDevCalcNumerator / (numEntries - 1)); } // Part D void print(double stndrdDev, double mean, string bankName) { cout << "===============================================" << endl; cout << "Bank Name: " << bankName << endl; cout << setprecision(1) << showpoint << fixed; cout << "Average wait time: " << mean << endl; cout << "Standard Deviation of the wait time list: " << stndrdDev << endl; cout << "===============================================" << endl << endl; } string getBankName(ifstream &inputFile, string &fileName) { string bankName; inputFile.open(fileName.c_str()); inputFile >> bankName; return bankName; } ``````

Current output:
 ``` Greetings! Enter number of banks would you like to analyze: 2 Please enter file name: alpha_bank.txt ```

^again nothing happens and it doesn't exit the program it just lets me press enter forever

How do I fix this?
Last edited on
Topic archived. No new replies allowed.