Programming Project I

Summer Session II, 1999



Project Objectives:

In this project you will learn how to operate the C++ programming environment. You will be given the source code to a program that you will need to type, save, compile, and link, so that it can be executed. The source code solves the problem described in the specification. When you type in the code as provided, do not make any modifications. You will also be provided with a sample input and output file to be used to test your program. REMEMBER, the input and output files from the Automatic Grader will be similar, not identical.

You will not be familiar with most of the code presented. Don't worry! We will cover them later during class. For now, try to understand the code, but concentrate mostly on getting it to compile and run.
 
 

What to Turn in and How:

Submit your code to the New Automated Grader (NAG). Follow the instructions for submitting to the NAG carefully. REMEMBER, don't change anything, because the NAG will match your output with the expected output.
 
 

Program Specification:

This program will read a series of floating point values from a single input file. These amounts represent dollar and cents change amounts that need to be returned to customers after a purchase. For each amount, the program should calculate the maximum number of quarters that can be given as change, followed by the amount of dimes (taking into account the amount of quarters returned), nickels (taking into account the quarters and dimes) and pennies (taking into account the quarters, dimes and nickels). For example, $2.34 of change would result in 9 quarters, 0 dimes, 1 nickel and 4 pennies being returned to a customer. The program will also print the total value of the change given back to customers. That is, if the input file contains $2.34, $0.25 and $5.00, the total change returned to the customer is $7.59.
 
 
 
 

Source Code:

// Id (Your info)
// Title: Changes
// Programmer Name: Pete dePasquale
//
// Honor Code
// On my honor:
//
// - I have not discussed the code in my program with anyone other than my instructor
// and the GTA assigned for this course.
//
// - I have not obtained code from another student.
//
// Purpose of the program
//
// This program is an introduction to C++ environment. It reads floating point values from
// an input file representing the amount of change to be returned to a customer after a
// sale. For each entry in the input file, change is to be given in quarters, dimes, nickels
// and pennies (maximizing the number of each coin given as change to the customer (for
// each input line).
#include <fstream.h> // Needed for file stream functionality
#include <iomanip.h> // Needed for floating point manipulation (output)

void main( ) {

// Variable Declaration section
float totalAmount + 0.0f; // Will hold the total amount of change read
float amount = 0.0f; // will hold amount of change to be given
ifstream inputFileStream; // Input File (input.dat)
ofstream outputFileStream; // Output File (output.dat)

// Open both the input and output file
inputFileStream.open("input.dat");
outputFileStream.open("output.dat");

// Activate the floating point manipulators
outputFileStream.setf(ios::fixed, ios::floatfield);
outputFileStream.setf(ios::showpoint);

// Set the precision of all float point numbers that are printed out
outputFileStream << setprecision (2);

// Write the first line of output to the output file
outputFileStream << "CS1044 - Project I" << endl;

// Write the program author's name
outputFileStream << "Program submitted by: Cayley Guimaraes" << endl;

// Read a floating point amount from first input line
inputFileStream >> amount;

// While the condition is true (there is more file)
// do the statements in between braces

while (inputFileStream) {

// Update the total amount of change read from input file
totalAmount = totalAmount + amount;

// Print a separator to the output file
outputFileStream << "=====================================================" << endl;

// Print the amount of change we need to return to the customer
outputFileStream << "Amount of change: $" << amount << endl;

// Reset the amount to be in term of cents, not dollar
amount = amount * 100;

// Print the number of quarters given as change
outputFileStream << " Number of quarters: " << (int) amount / 25 << endl;

// Update the amount to reflect having given X quarters to the customer
amount = amount - ((int) (amount / 25) * 25 );

// Print the number of dimes given as change
outputFileStream << " Number of dimes: " (int) amount / 10 << endl;

// Update the amount to reflect having given X dimes to the customer
amount = amount - ((int) (amount / 10) * 10 );

// Print the amount of nickels given as change
outputFileStream << " Number of nickels: " << (int) amount / 5 << endl;

// Update the amount to reflect having given X nickels to the customer
amount = amount - ((int) (amount / 5) * 5 );

// Print the number of pennies given as change
outputFileStream << " Number of pennies: " (int) amount << endl;

// Read the next amount from the input file
inputFileStream >> amount;
}

// Print a separator to the output file
outputFileStream << "=====================================================" << endl;

// Print the total amount of the change to the output file
outputFileStream << "The total amount of change was: $" << totalAmount << endl;

// Close the files
inputFileStream.close( );
outputFileStream.close( );
}

Input file specifications:

The input file (input.dat) should be located in the same directory as the running executable. The input file will contain up to 50 lines of floating point numbers, one per line. Each number represents the change that needs to be given to the customer after a purchase. Each amount is presented with two decimal values of precision and is not preceded by a dollar sign. There are no blank lines in the input file. Each number given is positive and is greater than 0.00.

Output file specifications:

The output file (output.dat) should be located in the same directory as the running executable. The output file will consist of the following lines: