Are there any C++ gurus here?

Pelagrin

Member
I'm taking a C++ class, and so far I love it.

We had our first "big" programming assignment assigned to use yesterday (10/07), and we have 2-weeks to complete it.  We were also warned that it is more difficult than anything we've done yet, and we had better not wait until the second week to do it.

Anyway, I finished the program today.
cool.gif
It is testing and working, but I was hoping there might be someone here to is a C++ expert who might be willing and able to critique it for me. (90% is on a working program, and 10% of the grade is on programming style)

I'm shooting for a perfect score on it, and I've got lots of time left to polish the thing up, so any comments, tips, or advice I can get would be greatly appreciated!
wink.gif
 
My proffessors were always big on comments. So make sure you have very descriptive comments everywhere. I always got marked off because I would assume that a portion of code was easy to understand. So be very liberal with the comments.
 
it looks very clean, and very well documented

There are only a few things I would have done differently

1) comments are sometimes below, sometimes above the item being commented upon. I would stick to one or the other (I personally perfer above the line so that you read the comment, and then see the line that is being commented upon). since there's no true standards for coding, the way you're doing it isn't wrong, it's just slightly harder to read.

2) variable names. This is a minor thing, but it makes code more readable especially in bigger programs (and readable code is good code)
you have for example
const double AdultPrice = 4.35;

I would use
const double dblAdultPrice = 4.35;

again, the way you're doing it isn't wrong, it's just that little change would make it easier to read, for when you're looking into the code you don't have to scroll back up to see what type you're variable is.


enjoy and God bless
 
In your costOneDayPark, you have two constants that are not capitalized:


double costOneDayPark(int numDays, int numAdults, int numChildren)
{
const double <span style='color:yellow'>adultPrice</span> = 45.30;
const double <span style='color:yellow'>childPrice</span> = 40.35;

return((numAdults * adultPrice + numChildren * childPrice) * numDays);

} // end of costOneDayPark


Also, I see you are validating that the user is entering the correct character in the menu, but no validation is performed on the other user supplied values. I think I could generate an application error by typing in alpha characters for your inputs for the number of tickets and other integer values.
 
There are good things and bad things about Hungarian Notation. In the example you picked out, there is already an identifier to the type of data the variable holds, and that is the keyword Price. Same thing with some of his other variables where it is prefixed with num to denote a number value.

One of the problem with Hungarian notation is the problem when a variable's type is changed. Take for instance I create an integer value and decide later that I need to keep track of decimal places and change it to a double. Not only do I have to change the type where it is declared but also change its name and every piece of code that references it. Now, say I simply make a very descriptive name that in the name it references that the data is numeric.

The problem comes with some of the other variables you have in your code like checkOutMonth and checkOutDay. It is not clear as to the type of data it holds. When you ask for a month, I could supply it as two very different values, January which is a string or 1 which is an integer.
 
Thanks for the feedback.

About the comments -- I normally prefer them above as well, but our professor always puts his comments basically where I put them. (below the function prototypes and then above everything else)

I figured that if I try to copy his programming style, he can't complain too much about me doing it wrong.

Thanks for catching the lowercase constants. I looked over the program and I missed that completely.

Our professor also told us we only needed to worry about validating in the one place, but I tested what you said and it does freak out if a character is entered. Since I have time, I'll probably throw in some validation. (maybe I'll just write some input functions since all three process functions basically use the same variables).

Thanks again!
 
[b said:
Quote[/b] (Plankeye @ Oct. 08 2003,1:26)]There are good things and bad things about Hungarian Notation.  In the example you picked out, there is already an identifier to the type of data the variable holds, and that is the keyword Price.  Same thing with some of his other variables where it is prefixed with num to denote a number value.

One of the problem with Hungarian notation is the problem when a variable's type is changed.  Take for instance I create an integer value and decide later that I need to keep track of decimal places and change it to a double.  Not only do I have to change the type where it is declared but also change its name and every piece of code that references it.  Now, say I simply make a very descriptive name that in the name it references that the data is numeric.

The problem comes with some of the other variables you have in your code like checkOutMonth and checkOutDay.  It is not clear as to the type of data it holds.  When you ask for a month, I could supply it as two very different values, January which is a string or 1 which is an integer.
Um...sorry, I think you lost me on that last post of yours. I'm not very familiar with Hungarian notation.


Two of the main things I wasn't sure about with the program was the way I handled the user input on the processLengthOfStay() function...

Since the user is supposed to enter the date as (mm/dd) or 03/12, I had to figure out what to do about them entering the slash. I probably missed in class when he said what to do, but I figured that they were entering a character and the program needed to do something with it, so I created a junk char variable called slash so it had somewhere to go. It worked, but was that the correct way to handle that?

...and I was unsure about how I programmed the daysElapsed() function. It seems to me that there's probably a better way to do it, but based upon the given assumptions, this way seemed to work. I guess the correct way would be to not count on those assumptions which I'm sure would end up requiring a WHOLE lot more work.
 
I think I figured out why my professor didn't require my class to validate all input. Basically, we've only learned a couple types of validation, and checking to see if something is a number instead of a character isn't one of them. I did a quick search on the net, and found a predefined function called isdigit(), but I'm not sure how I would use that, or if that would be what I wanted to use.
 
Hungarian Notation is a way of defining the data type of a variable in the variable's name. For instance, in Kidan's example, he used the prefix dbl for the Double data type. So you might use int for Integer values and str for String values. Again, it is just one of many ways to add some form of self commenting into your code. The problem I was trying to point out was the headaches you run into down the road when you need to change the data type.
 
Ah, I see. I was confused and was thinking for a minute that you were commenting on my program, rather than replying to someone else.
laugh.gif


I uploaded a new copy of my program to the link in the first post. I noticed that all three process functions were outputing pretty much the same thing, so I thought it might be a good thing to do as a function. The difference in the three outputs is that it outputs the type of ticket purchased.

Technically I don't know how to do strings yet (I'm not supposed to at least), but I got bored with the class' pace, so i'm actually two chapters ahead of them. Hopefully the teacher won't care that I included a string. *shrug*
 
Back
Top