473,322 Members | 1,781 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,322 software developers and data experts.

Feedback on my style

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.

#include<iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
double hworked;
double hrate;
double salary;

cout<<"Please enter hours worked ( -1 to exit):";
cin>>hworked;

if(hworked == -1){
cout<<"No records were processed.";
}
else{
while(hworked != -1){
cout<<"Please enter hourly rate:";
cin>>hrate;
if(hworked 40){
salary = (hworked - 40) * (hrate * 1.5) + (hworked - (hworked - 40)) * hrate;
}
else
salary = hworked * hrate;
cout<<"Employee salary is "<<salary<<endl;
cout<<"Please enter hours worked ( -1 to exit):";
cin>>hworked;
}
}

return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you could please give me some feedback on my programming style, I would appreciate it.
Thank you,

Nathan ;)
--
--------------------------------- --- -- -
Posted with NewsLeecher v3.9 Beta 1
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

Mar 2 '07 #1
6 1435
* dy****@hotmail.com:
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.

#include<iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
double hworked;
double hrate;
double salary;

cout<<"Please enter hours worked ( -1 to exit):";
cin>>hworked;

if(hworked == -1){
cout<<"No records were processed.";
}
else{
while(hworked != -1){
cout<<"Please enter hourly rate:";
cin>>hrate;
if(hworked 40){
salary = (hworked - 40) * (hrate * 1.5) + (hworked - (hworked - 40)) * hrate;
}
else
salary = hworked * hrate;
cout<<"Employee salary is "<<salary<<endl;
cout<<"Please enter hours worked ( -1 to exit):";
cin>>hworked;
}
}

return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you could please give me some feedback on my programming style, I would appreciate it.
Please replaces tabs with spaces before posting on Usenet.

I'll only comment on what you can improve.

First, you have a magic number, namely 40, in there. It should be a
named constant.

Second, the expression (hworked - (hworked - 40)) is identical to 40,
except for the academic possibility of overflow.

Third, you have duplicated code, the "Please enter hours worked" part.
That suggests using a different loop, one with exit in the middle. Like

for( ;; )
{
cout << "Please ..."; cin >hworked;
if( hworked == -1 )
{
break;
}
// ...
}

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 2 '07 #2
"Please replaces tabs with spaces before posting on Usenet."

What do you mean by this????????? I'm confused.

Nathan
--
--------------------------------- --- -- -
Posted with NewsLeecher v3.9 Beta 1
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

Mar 2 '07 #3
dy****@hotmail.com wrote:
if(hworked 40){
salary = (hworked - 40) * (hrate * 1.5) + (hworked - (hworked - 40)) * hrate;
}
else
salary = hworked * hrate;
In addition to having all those hard-coded values, this code is more
complicated than it needs to be. I'd do it like this:

if (hworked 40)
hworked += (hworked - 40) * 0.5;
salary = hworked * hrate;

Now salary is only calculated in one place, making it much easier to see
what's going on.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Mar 2 '07 #4
On Mar 2, 12:21 pm, Nathan (dyd...@hotmail.com) wrote:
"Please replaces tabs with spaces before posting on Usenet."

What do you mean by this????????? I'm confused.
He means your indenting makes the code hard to read in this group.
Prefer 2-4 spaces, not 8.

Cheers! --M

Mar 2 '07 #5
In article <G7******************************@giganews.com>, Nathan
(dy****@hotmail.com) says...

[ ... ]
cout<<"Please enter hours worked ( -1 to exit):";
cin>>hworked;
You may not have reached the point in the book where they've taught you
how to do so, but if you know about functions, I'd write a small one to
do this part, something like:

double get_double(std::string const &prompt) {
std::cout << "Please enter " << prompt;
double ret;
std::cin >ret;
return ret;
}

The rest could benefit from using descriptive names for more of what
you're doing:

double full_time = 40.0;
double overtime_multiplier = 1.5;

double overtime_hours = 0.0;

if (hworked full_time) {
hworked = full_time;
overtime_hours = hworked - standard_hours;
}

double standard_pay = rate * standard_hours;
double overtime_pay = rate * overtime_multiplier * overtime_hours;

double salary = standard_pay + overtime_pay;

As far as the structure of the program goes, I don't like the fact that
you ask the user to the hours worked in two different places. Above,
I've used a function to isolate most of that into one place, but that's
still not really sufficient (IMO). I'd prefer a structure more like:

while (-1.0!=(hworked=get_double("hours worked (-1 to exit): ") {
double rate = get_double("hourly rate: ");
std::cout << "Salary is: " << compute_salary(hours, rate);
}

where compute_salary contained code similar to the previous computation.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 3 '07 #6
In article <MP************************@news.sunsite.dk>,
jc*****@taeus.com says...

[ ... ]
The rest could benefit from using descriptive names for more of what
you're doing:

double full_time = 40.0;
double overtime_multiplier = 1.5;

double overtime_hours = 0.0;

if (hworked full_time) {
hworked = full_time;
overtime_hours = hworked - standard_hours;
Oops -- that should be:

overtime_hours = hworked - full_time;

Sorry 'bout that.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 3 '07 #7

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

Similar topics

7
by: George Hester | last post by:
Best done using Microsoft Internet Explorer but I believe Netscape may do OK not sure. I have one itsy bitsy little problem here. The tabbing? No forget that I got a few gray hairs with that and...
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...
10
by: dydx31 | last post by:
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: ...
16
by: dhtml | last post by:
Breaking up the FAQ has been discussed. http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/d11878ddfb2ac892/ I'm interested in doing this now. I propose something along the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.