Tip:
Highlight text to annotate it
X
Colin Goble: Hello everyone.
This demonstration is to, illustrate a program,
which, does calculations and arithmetic
and illustrates some of the concepts
of the second chapter of your book.
It is really an example of arithmetic and formatting
and how to do that.
It begins, of course, with the usual comments,
which provide the date, the author.
And I compiled this program with Bloodshed and Visual Studio.NET.
It seems to work fine with the both of those.
Unfortunately I didn't test it
with a Mac or in a Linux environment.
But it should be fine with Bloodshed and Visual Studio.
So as always with one of our applications,
we are going to start with a set of comments,
which indicates what the application does.
Now in this case, I will show you what it does
by executing the project.
We will compile and we will run the project.
It welcomes you to Colin's store
and asks what item you are buying.
Let's assume I want to buy some cheese.
Now let's suppose the unit price of cheese is, I don't know,
it's $5.25/pound, or something like that.
So we will enter 5.25
Let's assume I want to buy 2 pounds of cheese.
So the quantity is 2.
This program tells me
that the total price of my cheese is $10.50.
I happen to be living in a state with a 9.5% sales tax on food.
Of course that is a little tough, I suppose.
But there we are. So the sales tax in this case
happens to turn out to be $1.00 even.
And the total bill, therefore, is $11.50.
Okay, it is a simple calculation program,
it dialogues with the user.
It generates some output.
And when it is finished, will enter 'q' to quit.
Excellent.
Well, as always, we are going to need
#include iostream [pronounced: pound-include-eye-oh stream]
because we want to do input and output.
Because we want to do formatted input/output
in particular because our currencies
are always displayed to two places of decimals,
we are going to need to include
the so-called 'iomanip' [pronounced eye-oh-manip] library.
And because we are going to be inputting and outputting strings
for example, the name of the item being acquired,
we are going to need to include the string library, as I do here.
Now not all compilers require the string library.
I think Bloodshed probably does not.
Visual Studio I think, may.
So, it's never wrong,
and I will write the line: # include string
And then the program should work in both environments.
As before, we are going to include the namespace std.
So that everything, all of the objects
which aren't otherwise found, C++ is going to look
in the std namespace to find them.
And finally, I am going to declare
a constant, float double-precision floating-point.
I am going to call it STATE SALES TAX.
That is written in all caps as 'consts' in C++ are, by convention.
And then, we're in a state where the sales tax is 9.5%,
which of course mathematically is 0.095.
That is 9.5%.
We are going to use that later on in the program, by the way,
when we calculate the sales tax right here.
We are going to multiply by 0.095.
And the reason we want to make,
the reason we want to declare it this way
at the beginning of the program right here,
is so that it is easy to find if [laughs],
heaven forbid, the state should increase
or I suppose decrease the state sales tax.
We just have to look for it right here
in the header of our program,
as opposed to seeing a constant
that's buried down here much later on.
So if you have constants in your program,
this is the traditional way to do them.
The name of the constant is by convention, always in all caps.
Okay. As always, our main program must have the name 'main.'
It returns an error code to the operating system,
which is always of type 'int.'
In this case, the error code is zero
at the end of the program we return 0;
A zero error code indicates that the program terminated normally.
And a number other than zero would be used
in the case of an abnormal termination.
And in that case, it returns
the number that is returned is the error code,
which indicates the error that caused the abnormal termination.
Okay. So we need to declare some variables here.
They always start with a lower case letter.
Notice item total, sales tax, etc.
By convention variable names are always lower case.
If the name has multiple words in it,
then each subsequent word begins with a capital letter.
Now this is a convention, not a requirement of C++.
It is called 'camel case notation.'
And it is pretty widely used in the software industry.
Underbars [_] in names,
if I had decided to call this item
item _Name [pronounced under-bar name] like that.
C++ does allow underbars [_],
but they are no longer fashionable.
They were more commonly used in older C programs.
So we will tend not to use those in the duration of this class.
I will call it itemName like that.
So these are the names of the variables.
When you declare your intent to use a variable,
you must declare the name of the variable
and the type of the data that it's going to store.
So in this case, item name is a 'string,'
it is just a string of characters.
In the case of my example, it was cheese.
The item unit price has a fractional part.
It has a decimal part, if you will.
And there's two possibilities there.
I could have declared it 'float'
or I could have declared it 'double.'
I won't get into the details in this tutorial,
but for various reasons, you might as well
declare everything that has a fractional part 'double.'
Because on both Bloodshed and Visual Studio,
there is no performance or space penalty for doing that.
The quantity in the case of my example
has to be a whole number,
so I will declare it to be of type int, I-N-T.
The alternative there would be 'long', but again
there is no advantage to using a long integer.
They are both exactly the same size
in Visual Studio and Bloodshed.
So, if you are ever in doubt between int and long,
you might as well just choose int.
And as I said, if you are ever in doubt
between double and float, you might just as well choose double.
Okay. Now in the case of this example,
all of our, currency output
in order to look correct in the United States,
we need two places of decimals, dollars and cents.
This statement here, cout >]
point in the other direction.
I am going to be reading from the keyboard
into the variable called 'item name' [>> itemName].
Similarly, I am going to ask for and request the unit price.
We are going to do that here.
Then I am going to ask for the quantity
and read in the item quantity.
So it is a pretty straightforward dialogue
that steps the program through the dialogue procedure.
Now once we have had our dialogue with the user,
we need to calculate the results.
Obviously the total price is equal to the unit price,
multiplied by the quantity.
This represents an assignment statement in C++
as the kind of statement that does calculations.
The asterisk [*] of course in C++ is notation for multiplication,
just like a plus sign [+] here means addition.
So we are going to use an asterisk [*] for multiplication.
And the way to interpret this is to ask C++
to read the current value of itemUnitPrice
multiplied by the current value stored in the variable 'itemQuantity.'
And when it has performed that multiplication inside the computer,
we are going to store that result
in the variable whose name is 'itemTotalPrice.'
So as I said, this is an assignment statement.
The way to think about it is:
Evaluate the expression
on the right of the assignment statement
using the current values of the variables,
unit price and item quantity in this case.
And having done that calculation,
the computer will store the result, or associate the answer,
store the result in the variable whose name is itemTotalPrice.
The thing on the right of the equal sign
is moved and stored in
the variable that is on the left of the equal sign.
That is the way, quite simply, that we ask the C++ compiler,
or the computer, to perform a calculation.
So having calculated the itemTotalPrice,
we will calculate the sales tax with the same thing.
We will multiply the sales tax rate by the total price.
And of course the total price with tax then
is the total price of the item plus the sales tax.
So that assignment there calculates the total price with tax.
Okay. We have done all the calculations.
All we need to do now is show the user
what the results of those calculations were.
So, we are going to write
we are going to insert in the output stream,
this is the stream insertion operator.
'cout' remember, is a symbol that is associated with the screen.
We are going to take this literal stream
and we are going to write it to the screen.
Then we're going to take the current value
associated with the name item, with the variable 'itemName.'
Remember 'itemName' up here
was declared as being a string variable.
So we are going to take the current value
associated with the variable itemName
and insert that in the output stream.
And we're going to take some more literal text,
insert that in the output stream.
Then we're going to take the itemTotalPrice
and insert that in the output stream, followed by a new line.
Now by the way, this is all one statement.
Right here, it's a one statement
that has been folded over onto the second line.
So you might just notice there is no semicolon [;] here
at the end of this part,
the [;] which marks the end of the statement
appears there and there only.
'ItemTotalPrice' of course is an item,
'itemTotalPrice' is a, is a numerical quantity of type double.
[Clearing throat]
So when it is displayed,
it will be displayed with the two places of decimals
according to the formatting we set up earlier.
Then we are going to output the sales tax.
So here is a literal string
that we are going to write to cout.
We are going to take the current value
of the variable 'salesTax' and write that to cout,
also to two places of decimals.
Again, that is going to be followed
by a new line on the output screen;
that is on a line of its own.
And finally, we are going to output the total tax bill,
which is item 'TotalPriceTaxWithPrice.'
Again, these two lines represent one C++ statement.
So observe there is only one [;] here.
There is not a semicolon [;] on the line above.
And that is really it.
So then, that last thing is so that the item
doesn't flash off the screen.
As I showed you earlier, we are going to execute this program.
First of all we will compile it.
It compiled with no errors and no warnings.
The status is done.
And then, all that remains is to run it and see if it works.
Well, I don't know.
This time we will buy some eggs.
And the unit price of eggs we will assume
is $2.19 per box, let's say.
Let's assume I want to buy three boxes.
So my eggs are going to cost me $6.57, the sales tax is $0.62,
the total bill is $7.19.
We are paused on screen.
I will enter q followed by the enter key,
and we have tested our program.
At least that test has passed successfully.
Thank you.
I hope this helps you with your understanding of the chapter.