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:

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
#include <iostream> 
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>

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<char, std::char_traits<char> >::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
/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<char>]
assn4.cpp: In function 'double averageCalc(std::ifstream&, std::string&)':
assn4.cpp:86: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
/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<char>]
assn4.cpp: In function 'double stndrdDevCalc(std::ifstream, std::string, double)':
assn4.cpp:112: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::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<char>]
assn4.cpp: In function 'std::string getBankName(std::ifstream, std::string)':
assn4.cpp:146: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::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<char>]
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<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)/var/tmp//ccDFTpxe.o
stndrdDevCalc(std::basic_ifstream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, double)/var/tmp//ccDFTpxe.o
openAndVerify(std::basic_ifstream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)/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.
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
#include <iostream> 
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>

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
1
2
3
4
5
6
7
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:
1
2
3
4
5
6
7
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:
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
#include <iostream> 
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>

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.