Please enable JavaScript to use CodeHS

Fly Me to the Moon

By David Burnham

As SpaceX and NASA look to build the next rocket to go to the moon, many may wonder why it has taken so long to get back. Wasn't the Space Shuttle designed to travel between the Earth and the Moon?


In this introductory Hour of Code, we are going to take a look at the orbital mechanics behind what it would take to fly the Space Shuttle to the moon. Then, using a C++ program, we will write a basic program to calculate the fuel needed and see if the Space Shuttle would be able to make the trip to the moon!


This Hour of Code is designed for all students. No prior knowledge of computer science or C++ is needed, so let's get started with some of the basics!

Output in C++

We are going to create a program that can determine if a space ship can carry the necessary fuel to make an orbital change. To do this, we will need to ask the user for some information and then output the results.


Before getting into the program, let's look at how we can do basic input and output in C++.


Output in C++ uses the following command:

std::cout << "Hello World!";


There are a few thing to note about our output. First, what ever we want to print out to the screen must be inside double quotes. Second, C++ output doesn't automatically go to ta new line. If we want to add a new line after out output, we need to include \n at the end of out string.


Finally, you should note that all of our command lines end with a ;. This is how C++ knows to execute the command and without it, we will get an error. Take some time to play with the example below.

Space Orbit Calculator - Part 1

We are going to start building our Space Orbit Calculator. For this fist part, you are going to output a welcome statement and three questions. Don't worry about the answers yet, just print the questions. Here is what your output should look like:

Welcome to our Space Orbit calculator!
Please enter your starting orbit speed (km/s): 
Please enter your desired orbit speed (km/s): 
Please enter your maximum payload (pounds): 


Input in C++

To help make our programs more dynamic, we want to be able to read information in from the user. Before we read information, we need to have a place to store that information we read in. For that, we use variables. A variable in computer science is similar to one you may have seen in math in that it represents a value. In C++, there are many different types of variables, such as integers, decimal numbers, and strings (which hold a series of letters and characters). For today's lesson, we are only going to look at decimal number. In C++ we call these number float, short for a floating value number.


With that in mind, let's take a look at our basic input statement in C++:

float num1;
std::cin >> num1;


Notice the first line of our code is declaring that we will use a variable called num1 and that it is a float type variable. Line 2 is where we take out input and you should notice it looks very similar to our output statement, but now cout has become cin and the arrows change to show that the input is being stored in the variable. It is also important to note that the cin statement doesn't provide a prompt for the user, so we still need to use a cout statement to tell the use what we are prompting for. Take a look at the example below:

Space Orbit Calculator - Part 2

We are going to continue building our Space Orbit Calculator. For this second part, you should start by copying your code from part 1. In this part, you are going to create 3 variables to store the answers to the questions you created in part 1.


For each of the 3 questions you created, create a variable with an appropriate name and read the value in from the user. Here is a sample output:

Welcome to our Space Orbit calculator!
Please enter your starting orbit speed (km/s): 7.68
Please enter your desired orbit speed (km/s): 3.07
Please enter your maximum payload (pounds): 55000