Can I ask how do I call out this function? Is there any problem with code?
I tried running my program but nothing comes out. Just shows Press any key to continue...
Well, I think you need to show the rest of the code where you are calling the function. Of course if the condition tested in the if statement is not true, there would be no output.
In function main() this line void printChequeAcct(); is a declaration, it tells the compiler that the function exists. But you need to put this, in order to actually call the function:
printChequeAcct();
There are several other problems. Array acctTable[] is used without having been defined. The loop for (int i=0; i<30;i++) goes from index 0 to 29, but the array Table[] has only 10 elements.
Notice at line 19 the function is declared so the compiler knows what parameters it expects, and what type of value it returns.
Note also at line 15 the use of a defined constant SIZE. That makes it simpler to modify the program if required, as changing the value in just one place will affect everywhere that it is used.
I have a question. Even after reading I cant seem to solve.
Please take a look at my code. How can I traverse and declare so that I can carry out the xferAmount function
void xfer()
{
string fromaccount;
string toaccount;
double amount;
cout << "Please enter your Account Number to transfer: ";
cin >> fromaccount;
cout << "Please enter the Account Number you wish to transfer to: ";
cin >> toaccount;
cout << "Please enter the Amount you wish to transfer: $";
cin >> amount;
string reason;
int result = xferAmount (fromaccount, toaccount, amount, reason);
if (result != 0)
{
cout << "Failed: " << reason;
}
else
{
cout << "OK: " << reason;
}
}
int xferAmount (string fromaccount, string toaccount, double amount, string & reason)
{
if (fromaccount==toaccount)
{
reason = "The two accounts have the same account number and ""do not carry out the transfer of funds";
return 1; // indicate failed.
}
int from = -1;
int to = -1;
for (int i=0; i<SIZE; i++)
{
if (acctTable[i].account == fromaccount)
from = i;
if (acctTable[i].account == toaccount)
to = i;
}
if (from >= 0)
{
if (acctTable[from].balance - amount <= 10)
{
reason = "The transfer causes the balance of the first account to ""drop below $10 and do not carry out the transfer of funds";
return 1; // indicate failed
}
}
if ((from >= 0) && (to >= 0))
{
acctTable[from].balance -= amount;
acctTable[to].balance += amount;
reason = "Success";
return 0; // indicate success
}
reason = "Account number not found";
return 1; // indicate failed
}
Actually, I think a more modular approach would be to have a function like this which simply searches for the account number and returns the index of that account in the table, or -1 if not found.
1 2 3 4 5 6 7 8 9 10
int findAccount(string account)
{
for (int i=0; i<SIZE; i++)
{
if (acctTable[i].account == account)
return i; // success
}
return -1; // failed
}