By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

Feedback on my programming style

P: n/a
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;
double beg_bal;
double tot_charges;
double tot_credits;
double credit_limit;
double new_balance;

while(1){
cout<<"Please enter account number:(-1 to end)"<<endl;
cin>>account_num;
if(account_num == -1)
exit(1);
else{
cout<<"Enter beginning bal:"<<endl;
cin>>beg_bal;
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.

Thanks again,,

Nathan ;)
--
--------------------------------- --- -- -
Posted with NewsLeecher v3.7 Final
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

Nov 14 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Nathan wrote:
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;
double beg_bal;
double tot_charges;
double tot_credits;
double credit_limit;
double new_balance;

while(1){
cout<<"Please enter account number:(-1 to end)"<<endl;
cin>>account_num;
if(account_num == -1)
exit(1);
else{
cout<<"Enter beginning bal:"<<endl;
cin>>beg_bal;
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.
First of all, do not declare any local variables before you need them.
Second, initialise all your variables (at least in the beginning of
your career).

Having defined a branch that causes 'exit' (which is generally a bad
idea, it's better to return gracefully from any function, even 'main'),
you don't need to specify 'else', the unnecessary block causes
visibility confusion.

None of your input is checked for validity. What if I type 'abc' where
you expect a number? It may be better to define a "read a number" kind
of function for entering all those numbers.

Only one of your prompts specifies what value range is valid. Are
negative numbers accepted?

There is nothing in your program that requires or has any opportunity
for performance improvement, it's a UI program (most time is spent
waiting for user input) and there are very few calculations.

To make your code more readable:
Use spaces instead of tabs.
Surround binary operators (except dot and arrow) with spaces.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 14 '06 #2

P: n/a
dy****@hotmail.com wrote:
Here is a simple program that I wrote:
#include<iostream>
#include<iomanip>

int main(int argc, char* argv[]) {

while (1) {
int account_num = -1;
std::cout << "Please enter account number:(-1 to end)" << std::endl;
std::cin >account_num;
if (account_num == -1) {
break;
}
else {
double beg_bal = 0;
std::cout << "Enter beginning bal: " << std::endl;
std::cin >beg_bal;
double tot_charges = 0;
std::cout << "Enter total charges: " << std::endl;
std::cin >tot_charges;
double tot_credits = 0;
std::cout << "Enter total credits: " << std::endl;
std::cin >tot_credits;
double credit_limit = 0;
std::cout << "Enter credit limit: " << std::endl;
std::cin >credit_limit = 0;
double new_balance = beg_bal + tot_charges - tot_credits;

if(new_balance credit_limit) {
std::cout << " Acct number: " << account_num << std::endl;
std::cout << "Credit limit: " << credit_limit << std::endl;
std::cout << " New Balance: " << new_balance << std::endl;
std::cout << "Credit limit exceeded." << std::endl;
}
}
}
return 0;
}
I need some simple feedback: How is my programming style?
Can I improve anything in the code for better performance etc?
I don't think that your version worked the way that it was supposed to
work. Try mine instead.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '06 #3

P: n/a
Actually ,

My program works perfectly fine. I'm using microsoft visual studio
2005 to compile the code. Sorry if Im kind of new,,but im a novice
programmer. I'm using the Deitel C++ how to program book. They
didnt mention any other methods than the ones i used.

Thanks for your responses!

Nathan ;)
--
--------------------------------- --- -- -
Posted with NewsLeecher v3.7 Final
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

Nov 15 '06 #4

P: n/a
To add:

Minimize use of endl. endl flushes the stream which
in the case of

cout<<"Enter beginning bal:"<<endl;
cin>>beg_bal;

is not necessary since the first line must
appear on the screen before second line is
executed

cout<<"Enter beginning bal:\n";
cin>>beg_bal;

works just the same.

cout<<"Acct number: "<<account_num<<endl;
cout<<"Credit limit: "<<credit_limit<<endl;

can easily be changed to:

cout <<"Acct number: "<<account_num
<<"\nCredit limit: "<<credit_limit
<< endl;

typing '=' when you mean '==' is a common mistake
since if(account_num = -1) is well formed, a compiler
might not even issue a warning

prefer if(1- == account_num)
to if(account_num == -1)

so the compiler complains if you mistype

Nov 15 '06 #5

P: n/a
Instead of a while(1) loop that calls exit(), it's probably better to
do something like this:

cout<<"Please enter account number:(-1 to end)"<<endl;
cin>>account_num;

while(account_num != -1) {
...
cout<<"Please enter account number:(-1 to end)"<<endl;
cin>>account_num;
}
While this removes your exit(1) call, if you stayed with the above
program, you would probably want exit(0). Your program returning 0 is
interpreted by most (all?) operating systems as meaning your program
executed successfully. By returning 1, you are semantically indicating
that your program exited because it encountered an error, which is not
true. This is important if someone is running your program in a script
they are writing. They will want to check the exit code to make sure
it did what was expected, and it's assumed you will return 0 if it did.

Colin K.

Nov 15 '06 #6

P: n/a

dy****@hotmail.com wrote:
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.
Your function is kind of long. Good candidate for refactoring into
smaller ones. Google refactoring.

Nov 15 '06 #7

P: n/a
In article <11*********************@b28g2000cwb.googlegroups. com>,
dasjotre <da******@googlemail.comwrote:
>To add:

Minimize use of endl. endl flushes the stream which
in the case of

cout<<"Enter beginning bal:"<<endl;
cin>>beg_bal;

is not necessary since the first line must
appear on the screen before second line is
executed

cout<<"Enter beginning bal:\n";
cin>>beg_bal;

works just the same.
Potentially gain 0.01 sec in execution time
for a user interface module.

So I fail to see the point.

Flushing the stream can have advantages if execution
speed is not the first consideration. (think about
a multi-process application)
>cout<<"Acct number: "<<account_num<<endl;
cout<<"Credit limit: "<<credit_limit<<endl;

can easily be changed to:

cout <<"Acct number: "<<account_num
<<"\nCredit limit: "<<credit_limit
<< endl;
To me, the original version is more readable
and more maintainable but that's probably a matter
of taste.
>typing '=' when you mean '==' is a common mistake
since if(account_num = -1) is well formed, a compiler
might not even issue a warning

prefer if(1- == account_num)
to if(account_num == -1)

so the compiler complains if you mistype
Agree with that one.

Nov 15 '06 #8

P: n/a
On Nov 15, 8:09 am, "dasjotre" <dasjo...@googlemail.comwrote:
prefer if(1- == account_num)
to if(account_num == -1)

so the compiler complains if you mistype
Most people will notice the typo, but just in case:
if(-1 == account_num)
not
if(1- == account_num)

Colin K.

Nov 15 '06 #9

P: n/a

"Nathan" <dy****@hotmail.comwrote in message
news:45***********************@unlimited.newshosti ng.com...
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;
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 )
{
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;
else{
No need for the else. Even with your exit it wouldn't get here if the if
was true. So remove it.
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.
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.
>
Thanks again,,

Nathan ;)
--
--------------------------------- --- -- -
Posted with NewsLeecher v3.7 Final
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

Nov 15 '06 #10

P: n/a
Yannick Tremblay:
>>cout<<"Enter beginning bal:\n";
cin>>beg_bal;

works just the same.

Potentially gain 0.01 sec in execution time
for a user interface module.

An entire 10 million nanoseconds!

--

Frederick Gotham
Nov 16 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.