HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Calculating freefalling object's height

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
calculatingfreefallingobjectheight

Problem

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 
#include 

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

    std::cout > time;

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

    std::cout << "The rock fell " << feetFallen << " feet." << std::endl;
}

Solution

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 >> time)){
    std::cin.clear();
    std::cin.ignore(std::numeric_limits::max(), '\n');
    std::cout << "Invalid input.  Please enter a number: ";
}

Code Snippets

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

Context

StackExchange Code Review Q#62673, answer score: 10

Revisions (0)

No revisions yet.