Calculating freefalling object's height This is the code for my first CS1 project, I hope to be posting my code here to track my progress of learning the language of C++.  I shouldn't be too shabby, since I know C considerably well and have reviewed C++ code here before.


  Write a program that allows the user to enter a time in seconds and
  then outputs how far an object would drop if it is in freefall for
  that length of time.  Assume that the object starts at rest, there is
  no friction or resistance from air, and there is a constant
  acceleration of 32 feet per second due to gravity.  Use the equation:
  
  $$
\text{distance} = \dfrac{\text{acceleration} \cdot\text{time}^2}{2}
$$


rock.cpp:

/**
 * @file rock.cpp
 * @brief Determines the height that a rock falls (in feet) after a given time
 * @author syb0rg
 * @date 9/8/14
 */

#include &lt;iostream&gt;
#include &lt;cmath&gt;

int main(void)
{
    // initialize variables on declaration
    double time = 0;
    double acceleration = 32.174;

    std::cout &lt;&lt; "Enter the time in seconds that a rock will fall: ";
    std::cin &gt;&gt; time;

    double feetFallen = (acceleration * pow(time, 2)) / 2;

    std::cout &lt;&lt; "The rock fell " &lt;&lt; feetFallen &lt;&lt; " feet." &lt;&lt; std::endl;
}
Your code is both short and good, so there's not much to say.

Header organization

It's a good idea to keep your headers organized. Keeping them alphabetically ordered allows you to spot accidental duplicates.

main() signature

You don't need to put void in the parentheses. It's very much a C-ism, and is not necessary in C++. Empty parentheses also means the function takes no arguments.

const variables and constants

acceleration is a constant, and should be marked constexpr to indicate it is a compile-time constant. Please note that constexpr is only available in C++11 or later. If you have to use an earlier version, you can replace it with static const for largely the same effect. feetFallen should be marked const, since you only read from it after it is initialized.

Marking variables const and constants constexpr limits the number of moving parts in your application, making it easier to reason about and keep correct.

Unnecessary comment

Your comment about initialization is not necessary. The fact that you initialize variables the same time they are declared is obvious from reading the code.

Invalid user input

time will contain the value 0 if your user inputs non-numeric input. If you want to constrain the user to numeric-only input, you can loop until the user enters only a valid number. Inspired by this StackOverflow answer, your solution could look like this:

while(!(std::cin &gt;&gt; time)){
    std::cin.clear();
    std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
    std::cout &lt;&lt; "Invalid input.  Please enter a number: ";
}