|
← This "includes" code needed to use the "standard library" iostream. Libraries are repositories of compiled C++ code that we can use in our own programs. iostream provides input/output functionality. ← This allows us to use shorter names for standard library objects. For example, without this line we have to write "std::cout" rather than simply "cout". The line is saying "just assume the 'std::' part". ← The "main" function. For now, all our C++ code goes in between these { }'s. ← "cout << x" is how we write output. The right-hand side can be almost any expression. "std::cout" is the full name of the output stream associated with the terminal. "cout << endl" sends a newline to the output stream. ← A program actually returns a number to the command shell that launched it. Returning 0 is like saying "this program exited normally". |
Some more comments follow:
;
".
cout << x
The object x can be either a number or a string (a string is a sequence of
characters).
For short, you can print several things out in a single "cout" statement
by separating them with "<<", like this:
cout << "I am " << 44*12 + 3 << " months old!" << endl
cout << (3.55 - 17.017)*(3.55 - 17.017)*(3.55 - 17.017) << endl;This is a bit of a hassle. Normally we would think of first computing (3.55 - 17.017), then taking the resulting value and cubing it. Hopefully we think something like:
We need a variable x in which to store the value (3.55 - 17.017)! So we try creating the following program:
#include <iostream>
using namespace std;
int main()
{
x = 3.55 - 17.017; // The = operator assigns a value to the variable x.
cout << x*x*x << endl;
return 0;
}
When we compile this, we get an error message like
error line 4: 'x' is an undeclared identifierWhat the compiler is saying is this:
Look at the following statement:
double x;
int y;
=
operator assigns a value to the
variable x.
To make the program work, we write:
#include <iostream>
using namespace std;
int main()
{
double x; //declare x as a variable to store doubles
x = 3.55 - 17.017; //assign x the value 3.55 - 17.017
cout << x*x*x << endl; //print out x^3
return 0;
}
Once x has been declared as a variable of type double, it can be
used in the program just like a normal number, either to print
or to use in arithmetic expressions, like
cout << x*x*x << endl
, which does both.
What really goes on here is as follows:
double
, and
given the name x.
double x; x = 3.5; x = 2.4;... make perfect sense.
double x;
, space is reserved for x,
though we have no idea what actual value is in there - at this point x is
uninitialized.
x = 3.5;
copies the value 3.5 into the space
reserved for x.
x = 2.4;
copies the value 2.4 into the
space reserved for x, thus overwriting the 3.5 that had been there
previously. Think of x
as being the name of a box into which
double
values can be written.
cout
. In C++ (and in many other places) we refer to an
output stream, the idea being that each thing we write goes out
sequentially in the order we write it. In exactly the same way, we read
from an input stream.
cin
is really called std::cin
, but if we use using
namespace std;
we may drop the std::
part. (We
usually put the line using namespace std;
after the
#include
's.) It's defined in the iostream
library,
just like cout
.
Not surprisingly (given that our output stream is cout
) our
input stream object is called cin
.
Code | double x, y; cin >> x >> y; |
User Types | 12.0 25.0 |
Effect |
x gets the value 12.0, y gets the value 25.0. |
cin
<< 1.5
will cause a compiler error. (And what would it mean????)
double x
, cin
skips any spaces, tab's, or
newlines until it comes across a number. So, for example:
Code |
double x, y, z; cin >> x >> y >> z; |
User Types |
12.0 25.0 7.1 |
Effect |
x gets the value 12.0, y gets the value
25.0, and z gets the value 7.1 |
Putting this together, we can construct a very simple program Addition Calculator, which reads in two numbers
from the user and adds them together. Notice that the variable that
contains the sum of the two numbers input by the user is actually called
sum
. This is just to enhance the readability of my code. I
could've called the variable "George" and it would've worked just the
same.
double
s?
Libraries extend the functionality of C++. There is a set of standard
libraries that are an official part of the language (Here is some online
documentation on the standard libraries.), and the
iostream
library we've been using is one example.
cmath
that
allows you to perform other operations on double
s, like
square roots and sines and logarithms. (Here is some online
documentation on cmath
.)
double
, int
,
and return
. (A complete list can be found on this page.)
There are also non-keyword names that you are allowed but strongly discouraged
to choose for variables, since they are already used for important things.
Examples include main
, cin
and cout
. If
you use these names, they will confuse you. For example, what would happen
with the following:
double cin;
cin >> cin; // both cins will refer to the above double variable
As it turns out, the compiler will assume that both cin
's
refer to your new double
and you won't be able to use
cin
for reading. As we proceed, it will become
obvious what cannot be used as variable names.
Answer
and answer
will be considered different
variable names. A very common mistake that beginning programmers make is
to be sloppy in writing variable names, sometimes using capitals and
sometimes not. It is not good programming practice to use two variable
names that are spelled the same except for capitalization because it leads
to errors.
Try your best to write source code that is easy enough for mere mortals to understand (interpret this to mean the instructor grading your programs). Use meaningful variables names.
sqrt
function, so it includes a brief description of the
cmath
library. "Libraries" extend the base C++ language.
cin
and cout
.
double
data type.