Where is your main function? Where do you define bal? Where do you define Begbal? You are attempting to define your filename as a parameter to your function. You got to start with the basic framework and move on from there.
CODE
#include <fstream>
#include <iostream>
using namespace std;
// Declaration for Getbegbal. Void return type because we are passing file by reference.
// Whatever we do to stream will be reflected back in main. No need to return anything.
void Getbegbal(ifstream& in_stream);
int main() {
// Create a filestream
ifstream filename;
// Lets test if it is open (which it shouldn't be).
if (filename.is_open()) {
cout << "File is open" << endl;
filename.close();
}
else {
cout << "File is closed" << endl;
}
// Call our function to open the file.
Getbegbal(filename);
// Test again to see if the file is open. This time it is open, so we say it is open
// and close it.
if (filename.is_open()) {
cout << "File is open" << endl;
filename.close();
}
else {
cout << "File is closed" << endl;
}
}
// This function takes in a file stream and opens a file with it.
void Getbegbal(ifstream& in_stream)
{
in_stream.open("checkin.dat");
}
This little example took your code and modified it to show you how to open a file stream using another function. First we create the stream variable, then test to see if the file is open. It isn't since we haven't opened it yet. We then call our function and pass it the closed stream. In the function we open it and since we passed by reference, filename back in main is opened. So again we test if the file is opened and this time it is, so we say it is open and close the file stream.
Step through it and look at my in code comments to see what is going on. It should make some sense after you read through it a couple times.
Enjoy!
"At DIC we be file opening and closing code ninjas!"