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
#include <iostream>
#include <fstream>
usingnamespace 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?
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 = newchar[100];)