New Road Cost Estimator

C++ Console Output


Roads Needed!

Road

Imagine if your great uncle Ernie left you an inheritance of 16,000 acres of woodlands! Would you build a cabin? Or, would build a new neighborhood of homes? Either way, you're going to need a road to access what you decide to build. In addition, if there are any rivers or bayous to cross, then you'll also need a bridge.


Building Costs

While planning any building project, it's a good idea to estimate costs. Let us say that a ficticious road building company called Countryside Roads, LLC charges an initial fixed cost of $1,000 to start any road project. They also charge an additoinal amount depending on the length of the road. The variable costs for the lengths of road are:

Road Length Cost Per Mile
Less than 1 mile $2,500,000
1-5 miles $2,250,000
Over 5 miles $1,900,000

While that sounds like an arm and a leg to build a road, building bridges costs even more! If you want to build a bridge, Countryside Roads charges a fixed cost of $5,000 for any bridge project and then an additional amount per foot of bridge:

Bridge Length Cost Per Foot
Less than 100ft $1,500
100-500ft $1,300
Over 500ft $1,100


The Width of a River

River Width

Let's say your little plot of land has a wide river going right through it. You will need to calculate the distance across the river. Countryside Roads has a method for doing this easily. You must first measure the distance between two points on one side of the river. As seen in the drawing, you can call these two points A and B. From point A, choose a marker directly across the river so that the marker is at a 90 degree angle from the line AB. Then measure the angle from point B to the marker. You can estimate the distance across the river with these measurements.


Writing the code

With these costs and measurements in place, Countryside Roads can now estimate the costs of building your road. Countryside Roads would probably want a computer program that calculates these costs for them. This can easily be done in C++. While the width of the river sounds a little complex, it was actually quite simple. It took me only one line of code with the help of trigonometry. Figuring out how to code what to charge for the different lengths of roads or bridges was a little more lengthy, but still not difficult. I used if-else statements to determine which variable cost to use. When calculating distances, I declared variables using the double type, and for expenses I used long double just incase any very long numbers were needed. I also added another useful calculation; the total distance of roads and bridges built. Lastly, of course, I used cout to print to the console and cin to get input form the user.


Running the Code

Here's an example of what it looked like to run the code:

Welcome to Road Costs Estimator!
Enter your first name:
Bill
Bill, in order to estimate your costs, you will need to enter the number of miles your road will cover on both land and water.
For estimating distances over water, you must follow the instructions in the Road Costs Guide.
Enter the distance between the two points on one side of the river (in feet):
100
Enter the angle from the second point to the marker across the river:
85
Width of the river (in feet): 1143
The cost to build your bridge will be 1.2623 million dollars.
Enter the number of miles the road will cover on land:
4
The cost to build your roads on land will be 9.001 million dollars.
The total cost of your building projects will be 10.2633 million dollars.
The total distance of the planned road and bridge is 4.21648 miles!


Code for the Cost Estimator

The C++ program I wrote to do these calculations is written as follows:



For more information on C++, see the tutorials at LearnCpp.com

© 2019 Authentic Audio, LLC