"Nathan" <dydx31@hotmail.comwrote in message
news:455a3997$0$18267$9a6e19ea@unlimited.newshosti ng.com...
Quote:
Here is a simple program that I wrote:
>
//This program takes in various inputs.
//It calculates if the current balance exceeds the credit limit.
//Programmer: Nathan D. Brown
//Date: 11/14/06
>
#include<iostream>
#include<iomanip>
using std::cout;
using std::cin;
using std::endl;
>
int main()
{
int account_num;
|
Lets initialize this:
int account_num = 0;
Quote:
double beg_bal;
double tot_charges;
double tot_credits;
double credit_limit;
double new_balance;
>
while(1){
|
Do forever loops are ugly. I would prefer:
while ( account_num != -1 )
{
Quote:
cout<<"Please enter account number:(-1 to end)"<<endl;
cin>>account_num;
if(account_num == -1)
exit(1);
|
Exit is ugly, and with our while loop changed this would become:
if ( account_num == -1 )
continue;
No need for the else. Even with your exit it wouldn't get here if the if
was true. So remove it.
Quote:
cout<<"Enter beginning bal:"<<endl;
cin>>beg_bal;
|
What if they typed in a bal of "x"? beg_bal now has an undefined value (I
believe it's left to it's orignal value) and worst, our cin is now in a bad
state. For non trivial programs you need to check the result. Simplist
being:
if ( cin >beg_bal )
returns true if it took a value, is false if it couldn't read the input.
Not sure if
if ( ! cin >beg_bal )
works or not. I'll have to test.
Quote:
cout<<"Enter total charges:"<<endl;
cin>>tot_charges;
cout<<"Enter total credits:"<<endl;
cin>>tot_credits;
cout<<"Enter credit limit:"<<endl;
cin>>credit_limit;
new_balance = beg_bal + tot_charges -
tot_credits;
}
>
if(new_balance credit_limit){
cout<<"Acct number: "<<account_num<<endl;
cout<<"Credit limit: "<<credit_limit<<endl;
cout<<"New Balance: "<<new_balance<<endl;
cout<<"Credit limit exceeded."<<endl;
}
>
}
}
>
--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------
>
I need some simple feedback: How is my programming style? Can I
improve anything in the code for better performance etc?
ANY FEEDBACK YOU PROVIDE WILL BE GREATLY APPRECIATED!!!! I plan on
making programming into my career.
|
One thing I feel that changes a good programmer from a great programmer, is
error checking. Check *ALL* user input. Anything the user can input can
( and will ) be bad. It tends to make a simple one line like:
cin >account_bal;
into 5 or 6 lines, but your program becomes more robust and doesn't break.
You have to think, what if they typed in somethign that wasn't a number?
What do I want to do? Maybe I should just put it in a loop:
while ( ! cin >account_bal )
{
std::cout << "Bad input, please enter a value for account balance, -1 to
exit\n";
cin.clear(); // More may be needed than this
}
Over the years I've discovered that if you make an idiot proof program, they
come out with a better idiot, but you can do the best you can.
Quote:
>
Thanks again,,
>
Nathan ;)
>
>
--
--------------------------------- --- -- -
Posted with NewsLeecher v3.7 Final
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -
>
|