473,670 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>
#include<iomani p>
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_nu m;
if(account_num == -1)
exit(1);
else{
cout<<"Enter beginning bal:"<<endl;
cin>>beg_bal;
cout<<"Enter total charges:"<<endl ;
cin>>tot_charge s;
cout<<"Enter total credits:"<<endl ;
cin>>tot_credit s;
cout<<"Enter credit limit:"<<endl;
cin>>credit_lim it;
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."<<end l;
}

}
}

--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------

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 1680
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<iostre am>
#include<iomani p>
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_nu m;
if(account_num == -1)
exit(1);
else{
cout<<"Enter beginning bal:"<<endl;
cin>>beg_bal;
cout<<"Enter total charges:"<<endl ;
cin>>tot_charge s;
cout<<"Enter total credits:"<<endl ;
cin>>tot_credit s;
cout<<"Enter credit limit:"<<endl;
cin>>credit_lim it;
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."<<end l;
}

}
}

--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------

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<iostre am>
#include<iomani p>

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_l imit
<< 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_nu m;

while(account_n um != -1) {
...
cout<<"Please enter account number:(-1 to end)"<<endl;
cin>>account_nu m;
}
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************ *********@b28g2 000cwb.googlegr oups.com>,
dasjotre <da******@googl email.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<<"Credi t limit: "<<credit_limit <<endl;

can easily be changed to:

cout <<"Acct number: "<<account_ num
<<"\nCredit limit: "<<credit_l imit
<< 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...@googl email.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.news hosting.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<iostre am>
#include<iomani p>
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_nu m;
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_charge s;
cout<<"Enter total credits:"<<endl ;
cin>>tot_credit s;
cout<<"Enter credit limit:"<<endl;
cin>>credit_lim it;
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."<<end l;
}

}
}

--------------------------------------------------------------------
--------------------------------------------------------------------
-------------------

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1979
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 appreciate it if you could spare 5-10 minutes to fill out a survey and help us shape our creation to your needs. Those who do so before November 1 will get a free license when the product is released. The high-points:
18
1767
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
1620
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 which implements an interface InterfaceFuBar which contains two functions Fu and Bar. The goal is to do so without resorting to virtual functions. #include <iostream> template<typename SELF> struct ITABLE_InterfaceFuBar { void(SELF::*Fu)();...
1
14529
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 (Opera, Konqueror, etc.) So I need your feedback... Regards
2
7248
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 site and asks for help:) It seems to me this service could help me generate RSS for my static hompage site. So I did as this page told me, reformed my index page as: > * the title of the channel is taken from the title of the page
6
1546
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 for the last 10 years), so I would like you, the experts, to have a look at the code and provide me with your feedback. I do not know if I implemented good security practices or not and if I have made the functions efficient enough.
1
1269
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 anything I do in the best possible "style," even if it's just a silly experiment (e.g., a simulation to find out the average number of times you have to roll a die before all six sides have come up). With quickie experiments at home, alone, it's...
6
1446
by: dydx13 | last post by:
Hello there, Here is a program that I wrote in Visual Studio 2005: -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //Programmer: Nathan D. Brown //Date: 3-1-07 //This is exercise 2.19 from the Deitel book.
2
4619
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 5 #edit: not 4, mistake in the title // d3d_windowed program //header files to include #include <d3d9.h> #include <time.h>
0
8384
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8901
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6212
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4388
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1791
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.