Streams as arguments to functions?

So I'm trying to write a program that gets a whole bunch of numbers from a file called 'mean.txt', and find the average. I've tried sending the stream called 'ios' to a function, but it keeps telling me that "Identifier 'istream&' is unidentified" and that "Identifier 'ios' is unidentified"

Anyone know why this is happening?

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
#include <iostream>
#include <fstream>
double find_average(istream& ios); //I get 2 errors here telling me that 'istream' and 'ios' are unidentified
int main()
{
	using namespace std;
	ifstream ios;
	double average;
	ios.open("mean.txt");
	if (ios.fail())
	{
		cout << "Failed to open 'mean.txt'";
		system("pause");
		exit(1);
	}
	average = find_average(ios); //There's an error here that says I need a (pointer-to-) function
}
double find_average(istream& ios) //I get the same identifier problems here
{              //This line tells me that it expected a ';'     What's that all about????
	using namespace std;
	double next, sum, count, average;
	while (ios >> next)
	{
		sum = sum + next;
		count++;
	}
	average = sum / count;
	return average;
}
Do you know what a namespace is?

Put using namespace std; at line 3.
Thanks Moschops. I'd actually figured out the problem on my own.

But what if I wanted to use multiple namespaces in a single program? That's why I origianally had them in individual blocks. I know this program wouldn't need multiple namespaces, but I guess its just habit...
But what if I wanted to use multiple namespaces in a single program?
1
2
3
using namespace std;
using namespace someOtherNamespace;
using namespace andAnotherNamespace;


It's up to you to make sure there are no collisions. If there are, you can always go lower:
1
2
using std::iostream;
using someOtherNamespace::beansOnToast;


or just when you actually use them:
1
2
std::iostream inputStream;
someOtherNamespace::beansOnToast someVariable;

Last edited on
Topic archived. No new replies allowed.