// Filename: ALCHECK.CPP
// Asks the user for the number of check
// s written
// last month, then allocates that many
// floating-point
// values. The user then enters each che
// ck into the
// allocated array and the program print
// s the total
// after all checks are entered.
#include
intHowMany();
void GetChecks(int noOfChecks, float * theChecks);
float GetTotal(int noOfChecks, const float * theChecks);
void main()
{
int noOfChecks;
float total;
float * theChecks; // Will point to allocated memory
cout << "** Monthly Checkbook Program **" << endl << endl;
noOfChecks = HowMany(); // Ask the user how many checks
// Allocate the memory, 1 float per chec
// k
theChecks = new float [noOfChecks];
GetChecks(noOfChecks, theChecks); // Get the values
total = GetTotal(noOfChecks, theChecks);// Add them up
cout.precision(2);
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout << endl << endl << "Your total was $" << total
<< " for the month." << endl;
delete [] theChecks;
return;
}
//**************************************
// *********************
int HowMany()
{
int ans; // To hold the cin value
cout << "How many checks did you write last month? ";
cin >> ans;
return (ans);
}
//**************************************
// *********************
void GetChecks(int noOfChecks, float * theChecks)
{
int ctr;
// No need or vehicle for passing alloca
// ted memory. The
// memory does not go away between funct
// ions or blocks.
cout << endl << "You now must enter the checks, one at a time."
<< endl << endl;
for (ctr=0; ctr< noOfChecks; ctr++)
{
cout << "How much was check " << (ctr+1) << " for? ";
cin >> theChecks[ctr]; // Store value on the heap
}
return;
}
//**************************************
// *********************
float GetTotal(int noOfChecks, const float * theChecks)
{
// Add up the check totals
int ctr;
float total = 0.0;
for (ctr=0; ctr { total += theChecks[ctr]; } return total; }