473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Feedback on my programming style

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
10 1657
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
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
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
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
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

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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris Thiessen | last post by:
Hi, I'm Chris Thiessen. My partner and I need your help. We're finishing up what we think is a great compliment to the core Java API's, but we need feedback from other developers. We'd really...
18
by: Chris Mantoulidis | last post by:
There is a LARGE number of syntax styles in most (if not all) programming languages. For example, one syntax style (my current one): .... int main() { for (int i = 0; i < 50; i++) {
9
by: christopher diggins | last post by:
I would like some feedback if the follow code is standard compliant, and are there any obvious redundancies or inefficiencies? The purpose of the code is to emulate the behaviour of a struct FuBar...
1
by: Oliver Hoehle | last post by:
Hello! This ist the source-code for an editable combobox implemented with HTML,CSS and Javascript. I have tested it with IE and Mozilla. But I don't know, if it will work in other browsers...
2
by: Zhang Weiwu | last post by:
Hello. Today I found this free service by w3c generate RSS feed from xthml documents: http://www.w3.org/2000/08/w3c-synd/ This message described how this service failed to produce RSS for my...
6
by: aeldaly | last post by:
Hello all, I have just finished what I call Site Management System v0.0.0.0.0.0.0.0.0.1 :D I am not a professional programmer nor a really experienced one either (I've been dabbling with stuff...
1
by: Dgates | last post by:
I'm learning ASP.NET, C# and VB.NET, and hoping to get some feedback from more experienced programmers on a few issues regarding efficient, readable, well-organized code. I'm trying to program...
6
by: dydx13 | last post by:
Hello there, Here is a program that I wrote in Visual Studio 2005: ...
2
by: Netwatcher | last post by:
Hello, i am new to c++ windows and DX programming, i encountered a code in the book stated in the title, which doesn't work for a reason, here is the code // Beginning Game Programming // Chapter...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.