Hello,

I am writing this code to allow for a menu based banking system application. Part of the assignment was to check for input validation of the file I would like to open. In this case the user inputs their "customer id" which is stored as "custid" and then this is inputed into a general function -----> " inputFile.open("Cust_" + custid + ".dat"); ". For some when reason when I choose option 1 the input validation works, but does not work for option 2 and 3.

    {
            void accountInfo(string filename);
            void deposit(string filename);
            void withdraw(string custid);

int main()
{
string custid, cust_answer;
int choice;

cout << "Please enter the number that corresponds to the option you want to choose.\n"
    << "1.Display the account summary " << endl
    << "2.Deposit the amount into the account " << endl
    << "3.Withdraw the amount from the account " << endl
    << "4.Quit the program " << endl;

cin >> choice; 

while (choice < 1 || choice > 4)
{
    cout << "Please enter a choice from numbers 1-4 " << endl;
    cin >> choice;
}

switch (choice)
{
case 1:
    cout << "Please input your 3-digit user id \n";
    cin >> custid;

    accountInfo(custid);
    break;
case 2: 
    cout << "Please input your 3-digit user id \n";
    cin >> custid;

    deposit(custid);
    break;
case 3: 
    cout << "Please input your 3-digit user id \n";
    cin >> custid;

    withdraw(custid);
    break;
case 4: 
    exit(1);
    break;
}
}

void accountInfo(string custid)
{
ifstream inputFile;
string date, name;
double transactions, balance; 
balance = 0; 

inputFile.open("Cust_" + custid + ".dat");

if(!inputFile.is_open())
{
    cout << "This file was not found, please enter correct user id \n      \n";
    return;
}

getline(inputFile, name);  

cout << name << endl; 

while (true)
{
    inputFile >> date; 
    inputFile >> transactions; 

    if (inputFile.eof())
        break;

    balance += transactions; 
    cout << date << "\t" << transactions << endl;

}
cout << "Your current balance is: " << balance << endl << endl;
inputFile.close(); 
}

void deposit(string custid)
{
fstream inputFile;
double deposit_amount;

inputFile.open("Cust_" + custid + ".dat",fstream::out | fstream::app);

if (!inputFile.is_open())
{
    cout << "This file was not found, please enter correct user id \n\n";
    return;
}
cout << "Please enter the amount you would like to deposit " << endl;
cin >> deposit_amount;

if (deposit_amount < 0)
{
    cout << "Please enter a valid deposit amount that is greater than 0" << endl << endl;
        return;
}

SYSTEMTIME a;
GetLocalTime(&a);
inputFile << a.wDay << "/" << a.wMonth << "/" << a.wYear << "\t" << deposit_amount << endl;
inputFile.close();

accountInfo(custid);
}

void withdraw(string custid)
{
fstream inputFile;
string dummy;
double num, balance = 0;

inputFile.open("Cust_" + custid + ".dat");

if (!inputFile.is_open())
{
    cout << "This file was not found, please enter correct user id \n      \n";
    return;
}
getline(inputFile, dummy);
while (true)
{
    inputFile >> dummy;
    inputFile >> num; 

    if (inputFile.eof())
        break;
    balance += num;
}
inputFile.close();

double wd_amount;

inputFile.open("Cust_" + custid + ".dat",fstream::out | fstream::app);

cout << "Please enter in the amount you would like to withdraw: \n";
cin >> wd_amount; 

if (wd_amount > 0)
{
    wd_amount = 0 - wd_amount;
}

if (balance + wd_amount < 0)
{
    cout << "You do not have enough in your account balance to withdaw this amount \n\n";
    return;
}

SYSTEMTIME a; 
GetLocalTime(&a);
inputFile << a.wDay << "/" << a.wMonth << "/" << a.wYear << "\t" << wd_amount << endl;
inputFile.close();

accountInfo(custid);
}

    }

It's hard to say for sure where the problem is since the code you submitted won't compile. However, one place to start is in the declaration for inputFile. In accountInfo you declare it as ifstream but in deposit and withdraw you declare it as fstream.

On a side note, any time you see duplication in the code you're writing, breaking that out into a separate function, helps to eliminate these kinds of inconsistencies.

commented: The code wont compile? +0
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.