Using ifstream and Functions

I am doing an assignment and we are writing a function that needs to pass the ifstream by reference. However, I am not sure how to declare that. In short

[pseudo-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
#include <iostream>
#include <fstream>

using namespace std;

//declaring functions
void readAcc(ifstream&, char&, float&); //gets account info and initial bal

int main()    
{

//variables declared here

ifstream inc;
inc.open("bank.dat"); //opens data file

readAcc(ifstream& inc, char& ACCT, float& BAL);

//other code
//various formulas and loops which I will put in other functions later

inc.close();
return 0;
}
//*************************readAcc*******************************
void readAcc(ifstream& inc, char& ACCT, float& BAL) {
cout << "Welcome to The Bank.  What type of account will you be accessing today?" << endl;
inc >> ACCT;
cout << "What is your initial balance?" << endl;
inc >> BAL;
}


Am I declaring and calling the ifstream correctly? I am getting an error when I compile my code about the calling of the function and just wanted to know if that may be due to improper syntax?

Thanks,
A
Last edited on
In the function call you only give the argument, no the type. So the call would be:

readAcc(inc, ACCT, BAL);

However, ACCT and BAL are not defined yet, so you will still have problems.

So, before line 17, declare:
1
2
char ACCT;
float BAL;


By the way, ACCT will only contain a single char, not an entire word. If you are looking to read in a word, you need to use a string (#include <string> ) or a character array (char BAL[100]; or char* BAL = new char[100];)
Last edited on
Thank you.
Topic archived. No new replies allowed.