-
is this the best way??
I have written a little program from an example problem in a book. I am wondering if you guys would look at it and tell me if this is the most efficient way to do this or is there a better way?
Code:
#include <iostream.h>
int con, acctNum = 0;
double bal, totalCharge, totalCredit, credLimit, totalBal, newBal = 0.0;
int main()
{
while(acctNum != -1)
{
cout << "Enter account number(-1 to exit): \n";
cin >> acctNum;
if(acctNum == -1) {break;}
cout << "Enter beginning balance:\n";
cin >> bal;
cout << "Enter total charges:\n";
cin >> totalCharge;
cout << "Enter total credits:\n";
cin >> totalCredit;
cout << "Enter credit limit:\n";
cin >> credLimit;
cout << "Account: " << acctNum << "\n";
cout << "Credit limit: " << credLimit << "\n";
totalBal = ((bal - (totalCharge) + (totalCredit)));
newBal = ((bal + (totalCharge) - (totalCredit)));
if(newBal > credLimit)
{cout << "Credit Limit Exceeded.\n";}
cout << "Balance: " << totalBal << "\n";
}
}
-
Looks mostly ok to me. You could cin the various entries in one line, or two, rather than one for each entry.
cin >> bal >> totalCharge >> total Credit....
You could lose a few parantheses, but they don't hurt.
I think your totalBal calculation may be wrong. I don't think you should subtract charges and add credits, it should be the other way around. In fact, I don't see why you need totalBal at all, since you calculate newBal in the next line, and I think that's what you really want to print.