Let’s look at an example C++ program. This program will ask the user their weight on Earth, store that information, convert that weight to their weight on the moon, and then print their moon weight.
An object that weighs 1.0 pounds on Earth would weigh 0.1654 pounds on the moon.
In C++, multiplication is represented with the * symbol.
The program will save all the numbers it needs in variables. A variable stores a number or a mathematical formula.
For example:
x = 4;
y = 5 * x;
y = 0;
x = y;
y = 5;
At the end, `x` has `0` in it, NOT 5.
Types
C++ requires types for variables.
For example, a variable storing a number with a decimal should be a float, which stands for floating (decimal) point number.
Variables must be declared before they are used. Generally this is done by saying the variable type and then the names of all the variables that are that type at the first line of the function.
Here’s a quick reference of types in C++:
Type | Example |
---|---|
float | 0.124 or 4.0 |
int | 4 or 134 |
string | “Hello World” |
bool | true/false |
Let’s write an algorithm for this program.
-
Save the conversion factor (0.1654) as the variable
conversionFactor
-
Ask the user’s weight
-
Save the weight to a variable called
earthWeight
-
Multiply earthWeight and conversionFactor and save the result as the variable
moonWeight
-
Display
moonWeight
to the user
Getting User Input
This program will require input from the user. To get that input, use cin
.
cin
takes whatever the user types and assigns it to a variable. The syntax (the structure) of the command looks like this:
cin >> myVariable;
When the computer reaches cin
in the program, it will display a prompt for the user to type. The user can then type and hit the Enter key, and the computer saves whatever the user typed to the variable called myVariable.
Printing a Variable
To print out the person’s weight on the moon, use a second <<
, just like before endl
in the previous tutorial.
cout << "Something weighing" << earthWeight << "lbs on earth would would weigh "<< moonWeight << " lbs on the moon." << endl;
This sends the whole sequence of things to cout to be displayed.
Exercise 3.2.1: Full Program
//
is a comment.
Comments are ignored by the compiler and are used by programmers to explain parts of their code.
#include <iostream>
using namespace std;
int main(){
float conversionFactor, earthWeight, moonWeight; //define variables as floats
//Prompt the user to enter weight
//Note that there is no "endl", so the prompt will appear on the same line.
cout << "Enter your weight on earth:";
cin >> earthWeight; //Store what the user types as earthWeight
conversionFactor = 0.1654;
moonWeight = earthWeight*conversionFactor;
cout << "Something weighing" << earthWeight << "lbs on earth would would weigh "<< moonWeight << " lbs on the moon." << endl; //print out conversion
}
Operators
Some common math operators in C++:
Operator | Operation |
---|---|
+ | addition |
− | subtraction |
* | multiplication |
/ | division |
% | modulus (remainder) |
When you divide two integers, the remainder of the two numbers is left out. This is called truncation.
In integer division., 5 / 2
will give you 2, not 2.5. If you want the full value, you must use floats.
The %
(modulus) operator gives you the remainder of integer division.
So if you want the remainder of 5 / 2
(which is 1), you would say:
int rem;
rem = 5%2;
This is very useful in programming, since you can easily decide if a number is even, odd, or a multiple of some other number.
Exercise 3.2.2:
Write a program that takes a weight on the moon and converts it to Earth weight.
Exercise 3.2.3:
Write a program that asks the user for two numbers, and then prints out the sum of the two numbers. Hint: Use multiple cin commands.
Next Step
Proceed to “Functions”.