mercredi 23 mai 2018

C++ if statement not function properly

The problem is that the if statement just aren't functioning properly. Even when the option selected is 2, the first if statement runs. I want the first if statement to run when option is 1 and the second if statement to run if option is 2.Please let me know if you need more details or code. I am writing a bank account management system to help learn c++.

void depositWithdrawal (string number, int option)
{
int amount;
bool found = false;
bankAccount acc;
string accountNumber;
string name;
string balance;
string interestRate;
fstream File;
File.open("Account.csv");
if(!File)
{
    cout << "File could not be opened!";
    return;
}
while(File.good() && found == false)
{
    getline(File, accountNumber, ',');
    getline(File, name, ',');
    getline(File, balance, ',');
    getline (File, interestRate);
    if(accountNumber == number)
    {
        cout << "=============================================================================" << endl;
        cout << "Account Number      Name        Account Balance         Interest Rate        " << endl;
        cout << "=============================================================================" << endl;
        cout << accountNumber << "\t\t    " << name << "\t\t" << balance << "\t\t\t" << interestRate << endl;
        if(option == 1)
        {
            cout << "How much is being deposited? ";
            cin >> amount;
            int newBalance = stoi(balance) + amount;
            ofstream outFile;
            outFile.open("Account.csv", ios::app);
            outFile << accountNumber << "," << name << "," << newBalance << "," << interestRate << endl;
            outFile.close();
        }
        if(option == 2)
        {
            cout << "How much is being withdrawn? ";
            cin >> amount;
            if(stoi(balance) < amount)
            {
                cout << "insufficient balance" << endl;
            }
            int newBalance = stoi(balance) - amount;
            ofstream outFile;
            outFile.open("Account.csv", ios::app);
            outFile << accountNumber << "," << name << "," << newBalance << "," << interestRate << endl;
            outFile.close();
        }
    }
}

}

Aucun commentaire:

Enregistrer un commentaire