Thread: is this the best way??

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Lightbulb 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";
    	}
    	
    }
    Last edited by runtojesus; 11-07-2001 at 04:01 PM.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    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.

Popular pages Recent additions subscribe to a feed