Only after an analysis (the logical part of programming) has been completed and verified by a desk check, should a programmer consider the physical part of programming and move on to the act of translating the algorithm (in conformance with the other analysis documentation) into source code (statements written in a computer programming language.)
The source code shown below represents a translation of the analysis for the Problem Solving Example on this web site into C++. For an explanation of the statements in the source code below, study chapters 2 and 3 in your textbook and the coding examples on this web site.
/********************************
* Tax Calculating Program *
* Written by: Randy Gibson *
* Date: January 01, 2011 *
********************************/
#include <iostream> // holds definitions of cout and cin objects
using namespace std; // defines lang. vocabulary context for cin and cout
#include <iomanip> // holds definitions of setprecision() and fixed objects
#define RATE 0.065 // defines symbolic constant for the tax rate
int main ()
{
double PUR, // Purchase amount (in dollars)
TAX, // Sales tax (in dollars)
PAY; // Total payment (in dollars)
/*
The identifier RATE could be defined as a "named constant" instead of a "symbolic constant"
by removing the #define statement above and replacing it here with the declaration:
const float RATE = 0.065;
*/
/* Intro. & Instructions */
cout << "Sales Tax Calculator\n\n";
cout << "Written by Randy Gibson - 01 January 2011\n\n";
cout << "This program will request a purchase amount and then calculate\n";
cout << "the sales tax based on a " << RATE*100 << "% tax rate and display the result\n";
cout << "rounded to 2 decimal places.\n\n";
/* Data Input Section */
cout << "Enter the purchase amount: $";
cin >> PUR;
/* Calculation Section */
TAX = PUR * RATE;
PAY = PUR + TAX;
/* Output Section */
cout << setprecision(2) << fixed;
cout << "\nThe sales tax for that amount is: $" << TAX << endl;
cout << "The total payment with tax is: $" << PAY << endl;
return 0; /* Return zero error code to parent process */
}