SOURCE FILE: 010_firstSampProg.cpp
/*
This program inputs the cost of a product and its weight in ounces, and
then calculates and outputs the cost per ounce of the product.
*/
/* ******************************** */
/* ******************************** */
#include <iostream>
using namespace std ;
int main (void)
{
double cost ;
double weight ;
double cost_per_ounce ;
cout << endl ;
cout << "Please enter the cost of the product (fixed decimal format): " ;
cin >> cost ;
cout << "Please enter the net weight of the product\n" ;
cout << "in ounces (positive number, fixed decimal format): " ;
cin >> weight ;
cost_per_ounce = cost / weight ;
cout << "When the cost is " << cost << " and the weight is "
<< weight << endl ;
cout << "the cost per ounce is: " ;
cout << cost_per_ounce ;
cout << ".\n\n" ;
return 0;
}
/* ******************************** */
/* ******************************** */
/* Below is another copy of the program with lots of comments explaining
all the parts. */
#include <iostream>
using namespace std ;
int main (void)
{
/* Question: What happens if we insert a space
between < and iostream? */
/*
Variable declarations - the programmer chooses names for the
variables that make the program easier to read. However, the name
means nothing to the computer. The program works exactly the same,
for example, if we name the variables x, y and z.
C++ has rules about what strings of characters can be used as
identifiers: an identifier must begin with a letter or an
underscore, and only letters, underscores, and digits are allowed.
Certain keywords are reserved - like "if" and "while" - such
reserved words may not be used as programmer-defined identifiers.
C++ is case sensitive - that means upper case and lower case letters
are considered different - so Cost, COST, cost, and coSt would all
be considered different identifiers. However, normally it's a bad
idea to use identifiers that are different but appear to be the
same.
Be careful to initialize each variable before using it.
You can't rely on there being anything but"garbage" in a
variable, until after you have given it a value.
*/
/*
The three statements below allocate three locations in the primary
memory (RAM = volatile short term memory), and give each one a name.
In each location we can store a real number, like 2.349 or
893742.717.
*/
double cost ;
double weight ;
double cost_per_ounce ;
/*
One can often write output statements so that each output statement
writes one line of output, and that may be the logical way to do
things. However, one output statement can write several lines of
output, or only part of a line. Sometimes it's more logical to
program output like that.
*/
// Write a blank line
cout << endl ;
// Write something on the screen that prompts the user for input.
cout << "Please enter the cost of the product (fixed decimal format): " ;
/* Note that extraction operator (>>) below requires an
argument that designates STORAGE. */
/* Input what the user types and store a copy of it
in the variable named 'cost'. */
cin >> cost ;
/* Question: What happens if we use the forward slash below? */
// Write another prompt - for a net weight.
cout << "Please enter the net weight of the product\n" ;
cout << "in ounces (positive number, fixed decimal format): " ;
/* Input what the user types and store a copy of it
in the variable named 'weight'. */
cin >> weight ;
/*
Calculate the value of 'cost' variable, divided by the 'weight'
variable, and store the answer in the variable named
'cost_per_ounce'.
*/
cost_per_ounce = cost / weight ;
/*
TIMEOUT FOR MORE EXAMPLES
Some sample assignment statements to consider:
// This is a way to initialize while declaring
double x = 5.6, y = 3.2, z = 2.7 ;
int j ;
x = 30 ;
x = x + 4 ; // Does this one seem confusing?
y = x ;
y = x - 4 + 12 * (15 - z/2.4 ) ; // example of complex RHS
y = 5.6 ;
j = y ; // type conversion happens here - what is the value of j?
x = j+2 ; // type conversion happens here too - what is the value of x?
z = 3 / 2 ; // what is the value of z now? Why?
z = 3.0 / 2 ; // what is the value of z now? Why?
*/
/* Question: Can one C++ statement replace the four statements below? */
/* Write the values of two of the variables, with text around them.
What exactly does the code below write? That's a good
quiz question. */
cout << "When the cost is " << cost << " and the weight is "
<< weight << endl ;
/* Output the value in the cost_per_ounce variable */
cout << "the cost per ounce is: " ;
cout << cost_per_ounce ;
cout << ".\n\n" ;
return 0;
}