mardi 24 février 2015

Can someone help me find why an indirectly related global variable is affecting if statement result?

Testing Environment: Microsoft Visual Studio 2010 Ultimate.


What my if statement is suppose to do:



1. user makes a deposit OR withdraw----return 1 transaction
2. user makes a deposit AND withdraw----return 2 transactions
3. user does NOT make a deposit or withdraw----return 0 transactions.


My if statement half works. The reason I believe its wonky is because of my "inital balance" and my "new balance" couts that are using the global variable BALANCE.


The reason I feel like balance isn't directly related to the if statement is because only the Deposit amount (function DepositAmt) and Withdraw amount (function WithdrawAmt) should be directly affected by balance.


I went through the main.cpp and I've tried rearranging order of code and it's not making a difference. I'm completely stumped.



**Main.cpp**


int main()
{
char firstName[255];
char lastName[255];
char sinNumber[255];
double balance = 0;
int accountType = 0;
int transactions = 0;

//Retrieve client information
...
cout << "Enter initial balance: ";
cin >> balance;

//Creating the account
Account account(firstName, lastName, sinNumber, balance, accountType, transactions);

double deposit;
double withdraw;
double amount;
double ammount;
int transaction;

cout << "Amount to deposit: ";
cin >> amount;
deposit = account.DepositAmt(amount);
cout << "Your new balance is: " << deposit << endl;
cout << "Amount to withdraw: ";
cin >> ammount;
withdraw = account.WithdrawAmt(ammount);
if (deposit >= 1 && withdraw >=1)
{
transactions = transactions + 2;

}
else if (deposit >= 1 || withdraw >=1)
{
transactions = transactions + 1;

}
else
{

};
cout << "Your new balance is: " << withdraw << endl;
cout << "Transactions: " << transactions << endl;

}


***account.cpp***

#include "account.h"

//Initializing account entities
Account::Account(char fn[], char ln[], char sin[], double bal, int actype, int trans)
{
strcpy(firstName, fn);
strcpy(lastName, ln);
strcpy(sinNumber, sin);
balance = bal;
accountType = actype;
transactions = 0;
};


//Deposit amount to account of user, will return the balance
double Account::DepositAmt(double amount)
{
if (amount < 0)
{
cout << "You cannot deposit a negative balance!" << endl;
return -1;
}

//the initial balance will be added to the amount deposited
balance = balance + amount;

return balance;

};

//Withdrawal amount from account of user
double Account::WithdrawAmt(double withdrawal)
{

//Withdraw more than balance, cause error
if (withdrawal > balance)
{

cout << "Sorry the withdrawal amount exceeds the account balance" <<endl;
return balance;
}

//withdrawal will deduct from the current balance.
balance = balance - withdrawal;

return balance;

};


Test Scenarios : Test 1 Test 2 Test 3 Test 4 Test 5


(access to full code :

http://ift.tt/1LBc6om account.h


http://ift.tt/1zdFJFo account.cpp


http://ift.tt/1zdFJFq main.cpp )


Thank you.


Aucun commentaire:

Enregistrer un commentaire