MCC - Marquez - CIS162AB - C++ Level I
P06_ex_C Sample Program using C Libraries
   cpp.gif

This program is an example of P06-ex call-by-reference rewritten uisng C libraries. C++ is very similar to C, but they differ in input/output operations, and pointers must be used instead of call-by-reference.
Sample Output:

P06_ex_C - Juan Marquez    TR 1:00pm

Enter a value between $5 and $15.00 for the price: 12.50

Enter a value between 1 and 50 for the quantity: 30

Price:          12.50
Quantity:       30
Subtotal:       367.50
Sales Tax:      18.38 at 0.05
Shipping:       10.00
Total Due:      395.88
Net Price:      13.20

Thank you!

Press any key to continue


Source Code:

/*P06_ex_C Sample Program using C libraries - Juan Marquez   TR 1:00pm

C notes:

    The single line comment symbol (//) is not supported by C.

    Much more consideration to data types (int or double).
    Much more attention to input and output of formatted values.
    To print formatted text,  the text must be defined as a string using
        formatting codes which are preceded by a % character.
        A format code is placed in the string where the value of the
        variable should be displayed, and the list of variables
        are then passed as agruments along with the formatted string.

    Formatting codes:
        %s = string
        %d = integer
        %f = floating point
*/

/*C++
#include <iostream>  // cin and cout
using namespace std;
*/

#include <stdio.h>   /* scanf(), printf(), and file I/O */

/*
   call-by-value work the same way, but pointer 
   variables are used for call-by-reference. 
   Pointer variables are special variables that store a memory address. 
*/

double inputPrice(); 
void inputQuantity(int *quantity);
void calcCost(double price, int quantity, double *subtotal); 
void calcTax(double subtotal, double taxRate, double  *amount);


/*Declare global constants  */
    const double DISCOUNT_RATE = 0.98;


void main()
{
/*Declare local constants */
    const double SHIPPING = 10.00, TAX_RATE = 0.05;

/*Declare local variables */
    int  quantity;
    double price, subtotal, salesTax, total, netPrice;

/*C++ only
// Set the decimal point to 2 positions
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
In C the precision is included in each printf command with %.2f

    cout << "P06_ex - Juan Marquez    TR 1:00pm \n\n";
*/
    printf("P06_ex_C - Juan Marquez    TR 1:00pm \n\n");

    price = inputPrice();       
    inputQuantity(&quantity);   /* Use the & to get the address of the variable */
                                /* instead of the value stored at that location. */

//Perform the calculations
    calcCost(price, quantity, &subtotal);
    calcTax(subtotal, TAX_RATE, &salesTax);

    total    = subtotal + salesTax + SHIPPING;
    netPrice = total / quantity;

//Display the results using format codes in the string.
    printf ("\n");
    printf ("Price:    \t%.2f \n",price);
    printf ("Quantity: \t%d   \n",quantity);
    printf ("Subtotal: \t%.2f \n",subtotal);
    printf ("Sales Tax:\t%.2f at %.2f \n",salesTax,TAX_RATE);
    printf ("Shipping: \t%.2f \n",SHIPPING);
    printf ("Total Due:\t%.2f \n",total);
    printf ("Net Price:\t%.2f \n",netPrice);

    printf ("\n\nThank you!\n\n");

    return;
} // end of main


//Function Definitions

double inputPrice()
{
    double price;

    do
    {
        printf("Enter a value between $5 and $15.00 for the price: ");
        scanf("%lf",&price);

    } while (price < 5.0 || price > 15.0);

    printf("\n");
    return price;
}


void inputQuantity(int *quantity)
{
    *quantity = 0;

    while (*quantity < 1 || *quantity > 50)
    {
        printf("Enter a value between 1 and 50 for the quantity: ");
        scanf("%d", quantity);
    }

    return;
}


void calcCost(double price, int quantity, double *subtotal)
{
//DISCOUNT_RATE is a global variable.

    if (quantity > 25)
        *subtotal = quantity * (price * DISCOUNT_RATE);
    else
        *subtotal = quantity * price;
    return;
}


void calcTax(double subtotal, double taxRate, double *tax)
{
    *tax = subtotal * taxRate;
    return;
}

Revised: 08/15/2005 - www.mc.maricopa.edu/~marquez/cis162ab/p06_ex_c.html