I signed up for a C++ class during my graduate studies in 2002. The class was cancelled, I took Java instead, and the Deitel & Deitel "C++ How To Program" 3ed has been on the shelf since. So after 12 years I'm trying to work through the book on my own...
Problem 3.12 requires computing parking charges with a specific formula (min $2 charge for 3 hrs or less, .50/hr after 3 hrs with $10 max for 24 hrs) for three cars with the parking time already coded in the program using a calculateCharges function and formatting the output. No problem. This is before the array chapter. Now that I know some array material I went back to make this program take console input and output only the relevant data.
Everything works fine, but I suspect the code isn't very elegant. Did I integrate an array the hard way? Should I have used a multidimensional array instead? Any style or programming tips? Realizing there are many ways to skin a cat, I want to make sure I'm doing this as correctly as possible before I continue on. Hopefully this isn't bad for one week's free time. Thanks.
#include <iostream>
#include <iomanip>
#include <math.h>
usingnamespace std;
double charge = 0; // amount charged to a vehicle
double calculateCharges (double); // prototype function
int main(int argc, constchar * argv[]) { // default template from Xcode6
int car = 1; // variable to tell data entry clerk which car's hours to enter
double hours[8]; // array to hold parking hours
double charge[8]; // array to hold charge for each car
double totalCharge = 0; // total of all charges for 24 hour period
double totalHours = 0; // total of all hours parked for 24 hour period
for (int i = 0; i < 8; i++) { // increment variable to step through array
cout << "Enter the hours for car " << car << " (-1 to end): "; // user prompt for specific car
cin >> hours[i]; // hour entry
if (hours[i] != -1) { // user enters -1 to exit data entry
charge[i] = calculateCharges(hours[i]); // 'charge' receives amount returned by function
totalCharge += charge[i]; // add charge to total
totalHours += hours[i]; // add hours to total
car++; // increment car counter
}
else {
hours[i] = NULL; // erase the -1 entered to exit the program
break; // exit the FOR loop
}
}
cout << "Car Hours Charge\n"; // header text
for (int i = 0; i < car - 1; i++) { // step through array to print data to console
cout << i+1 // print car number
<< setw(15) << right << setiosflags(ios::fixed) << setprecision(1) << hours[i] // print hours
<< setw(15) << right << setiosflags(ios::fixed) << setprecision(2) << charge[i] << "\n"; // print charge
}
cout << "TOTAL" // print total row
<< setw(11) << right << setiosflags(ios::fixed) << setprecision(1) << totalHours // print total hours
<< setw(15) << right << setiosflags(ios::fixed) << setprecision(2) << totalCharge << "\n"; // print total charges
return 0; // return
}
double calculateCharges (double hours) { // function to compute charge based on hours parked
int chargeHours = 0; // number of hours charged
chargeHours = floor(hours + 0.5); // round time parked to next integer hour
if (chargeHours <= 3) {
charge = 2; // if parked less than 3 hours, charge $2
}
else {
charge = ((chargeHours - 3) * 0.5) + 2; // subtract 3 hour amount and add $0.50/hour
if (charge > 10) {
charge = 10; // if over $10, replace amount owed with $10
}
}
return charge; // return calculated charge for car
}
You have many unnecessary comments.
Don't use a "raw" array. Use std::vector instead.
Don't use NULL in c++ for non pointers. (In fact for pointers use nullptr or 0(if you are not using c++11) )
Try not to use global variables.
Thanks. Now that I'm several weeks into the book, many of my comments are superfluous. Haven't covered vectors or pointers yet. Also now understand the implication of global variables (and convenience of local ones). Currently in week four of an edX C++ course, which is better than struggling alone.
Thanks again.
You can use round(hours) instead of floor(hours+0.5)
You have two variables (i and car) that basically indicate the number of cars entered. You should use just one.
As arv7 suggests, charge should be a local variable in calculateCharges()
Although many of your comments are unnecessary, it's usually better to have too many than not enough. What's missing however is a description of what the program is. I suggest you get in the habit of putting this in the comments. Imagine that someone (you perhaps) picks up the code in 10 years. What is it?
In this case, a simple comment at the top will do: // Problem 3.12 in Deitel & Deitel's C++ How To Program
In a more realistic example you'd want to explain the problem being solved. Also, you would probably want a comment in calculateCharges() that explains the formula: // $2 for first 3 hrs. Then 50 cents/hr with a max charge of $10.
Oh, and regarding arrays vs. std::vector, vectors expand automatically and they are safer because they check whether an index is out of bounds. Arrays on the other hand generate smaller, faster code. The right choice depends on the circumstances.
Vectors may or may not check for an index out of bounds, depending on how you choose to use them. This choice may affect whether of not the code is slower than a plain array.
I've now read several articles on code commenting and see how I could have done better. Also now understand how to use variables more effectively and efficiently.