Section 2.3 of Problem Solving With C++
Lecture
Suppose I want to compute something like (3.55  17.017)^3. I can do this with C++, but "^" doesn't mean exponentiation in C++, so instead I need to write:
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: "let x be (3.55  17.017) and compute x^3." We need a variable in which to store the value (3.55  17.017)!
So we try creating the following program: 
#include <iostream> int main() { x = 3.55  17.017; std::cout << x*x*x << std::endl; return 0; } 
When we compile this, we get an error message like
error line 4: 'x' is an undeclared identifier
What the compiler is saying is this: "x??? you never told me there was going to be an x!" If you want to use x to store the value (3.55  17.017), you need to first tell the compiler that the name x is going to stand for a number  this is called declaring the variable x.
The
statement double x;
declares
that x is a variable of type double, which means that it's a variable
that can stand for numbers with decimal points. The type of a variable
tells you what kind of objects can be stored in the variable. There are many
different types in C++, and in fact understanding types is one of the
most important skills you'll learn in this course.
The
=
operator assigns a
value to the variable x.
To make the program work, we write: 
#include <iostream> int main() { double x; //declare x as an object x = 3.55  17.017; //assign x the value 3.55  17.017 std::cout << x*x*x << std::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 that space in your computer's memory is allocated for
one object of type double
,
and given the name x. When you ask for the value of x (by
involving it in an arithmetic expression or by trying to print it) the computer
fetches a copy of the value from memory. When you use the = operator, the
computer takes the value on the right hand side, and copies it into the space
reserved for x.
Thus, strange sequences like:
double x;
x = 3.5;
x = 2.4;
...
make perfect sense. After the statement double
x;
, space is reserved for x, though we have no idea what actual
value is in there  at this point x is uninitialized. The statement x = 3.5;
copies the value 3.5 into the
space reserved for x. Finally, the statement x = 2.4;
copies the value 2.4 into the
space reserved for x, thus overwriting the 3.5 that had been there
previously.
Legal Variable Names
Can you use any word or letter to represent a variable name? The answer is NO. A variable name must begin with a letter (lowercase or uppercase) or an underbar ( _ ). It may be any length you want (Anything after the first 32 characters will be ignored.) but it can only contain letters, digits, and the underbar. Except in special situations, the use of the underbar to begin a variable name should be avoided. There are a special class of keywords reserved for use by C++ and you may not use one of them to name a variable. Examples of keywords are double, cout, and endl. As we proceed, it will become obvious what keywords cannot be variable names but a complete list can be found in Appendix A of your textbook.
C++ distinguished between uppercase and lowercase. As a result, 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 variables names that are spelled the same except for capitalization because it leads to errors. Your source code will be more easy to understand by mere mortals (interpret this to mean the instructor grading your programs) if you use meaningful variables names.
We've
already seen how to output information from a program using 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. Not surprisingly (given that our output
stream is cout
) our input
stream object is called cin
.
[Note: Really it's called std::cin
,
but if we use using namespace std
we may drop the std::
part.
It's defined in the iostream
library, just like cout
.]
Code 
double x, y; cin >> x >> y; 
User Types 
12.0 25.0 
Effect 

You
may only read into variables, so cin
>> 1.5
will cause a compiler error. (And what would it
mean????) When reading into 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 

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?Very
few of the mathematical operations we typically use with real numbers are part
of the core C++ language. In fact, the only operations you'd recognize from
math class are +, , *, and /. Operations like exponentiation, square roots,
logs, and so on are not part of the core language, but they are available to
you in libraries. 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. There is a library called cmath
that allows you to perform other
operations on double
s, like
square roots and sines and logarithms.
Here is a list of problems and solutions. With even the very basic construction of input, output, variables, and expressions, we can write some useful programs.
· Fahrenheit  Celsius Conversion
·
Roots of Quadratic
Polynomials This requires the sqrt
function, so it includes a brief description of the cmath
library. "Libraries"
extend the base C++ language.
· Compound Interest Calculation. There are two solutions given for this problem, Version 1, and Version 2. Version 2 uses variable reassignment for the first time.
Last modified by LT M. Johnson 08/15/2007 09:11 AM