So far, I have only read about using void functions to take on ifstreams as a parameter. Yet I want to be able to make a single function that takes a file of numbers as a parameter, and do some calculation to each number. I can do this with a void function pretty easily, but I want a separate the calculations and the opening of the file. How do I do this?

for an example Ill show you these prototypes.
//works
void average( ifstream& file_in);
//doesnt work to my knowledge
int average(ifstream& file_in);

Ps I am away from my computer with my code, and my compiler.
No arrays please I havent learned that yet!

>//doesnt work to my knowledge
>int average(ifstream& file_in);

The parameters and return type are not dependent. If that prototype doesn't work, it's because you did something else wrong. How about posting a complete program that fails?

Ill be away from my comp with the program for a few hours, but thanks for the answer. I spent like an hour google searching for that and found nothing.

here is the code

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;

void open_file(ifstream& fin);
//Precondition: The File Written.dat must exist
//Postcondition: opens that file

double calc_average(ifstream& fin);
//Precondition, file must be opened and linked
//Takes the average of a list of numbers


int main()
{
    double average;
    ifstream file_in;

    open_file(file_in);
    average=calc_average(file_in);

}




void open_file(ifstream& fin)
{
    fin.open("written.dat");
    if (fin.fail())
    {   
        cout<<"fin failed line 26";
        exit(1);
    }   
}




double calc_average(fin)
{
    double next, sum;
    int count=0;

    while (fin>>next)
    {   
        sum=next+sum;
        count++;
    }   

    return (sum/count);
}

call your calc_average inside openFile function.

main function should return a value

int main()
{
		.
		.
		.
		.
		.
		.

		return 0;
}

double calc_average(ifstream& fin)

double calc_average(fin)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.