//
// Program:  volume_improved
// Author:  B. Massingill
//
// Purpose:  test "volume" function (with error checking).
//
// Input:  program prompts for one set of inputs to volume function.
// Output:  program prints results of calling volume function with
//     some fixed sets of input, plus user-supplied input.  each set 
//     of input is checked to see that it meets the function's 
//     precondition; if it does not, an error message is printed and
//     the function is not called.
//

#include <iostream.h>		// has input/output functions
#include <stdlib.h>		// has EXIT_SUCCESS

// ---- Function prototypes (should precede main program).

// Compute volume of a box.
// Pre:  h, w, d all positive.
// Post:  return value is volume of box with dimensions h, w, d.
int volume(int h, int w, int d);

// Test volume function.
// Pre:  none.
// Post:  if h, w, d all positive, volume(h, w, d) printed to standard 
//     output, with explanatory text; otherwise error message printed.
void try_volume(int h, int w, int d);

// Get a positive integer from standard input (prompt user).
// Pre:  none.
// Post:  user has been prompted to enter positive integer, return
//     value is what was entered.
// (This is a rather silly function, included mostly as an example
//     of a function with a return value but no input parameters.
int get_positive(void);

// Print some text before prompting for box dimensions.
// Pre:  none.
// Post:  text printed to standard output.
// (This is a rather silly function, included mostly as an example
//     of a function with no input parameters and no return value.)
void print_prompt(void);

// Print error message.
// Pre:  none.
// Post:  error message indicating that n is not positive printed
//     to standard output.
// (This is also a somewhat silly function, included to avoid
//     duplicating the error message.)
void print_not_positive(int n);


// ---- Main program.

int main(void)
{
  try_volume(1,1,1);
  try_volume(2,3,4);
  try_volume(-1,2,3);
  try_volume(1,-2,3);
  try_volume(1,2,-3);

  int height, width, depth;
  print_prompt();
  height = get_positive();
  width = get_positive();
  depth = get_positive();
  try_volume(height, width, depth);

  return EXIT_SUCCESS;
}


// ---- Function definitions (should follow main program).

//      For function documentation (descriptions, preconditions
//      and postconditions), refer to corresponding function
//      prototypes above.

int volume(int h, int w, int d)
{
  return h*w*d;
}

void try_volume(int h, int w, int d)
{
  cout << "Box dimensions:  " << h << " by " << w << " by " << d << endl;
  if (h <= 0)
    print_not_positive(h);
  else if (w <= 0)
    print_not_positive(w);
  else if (d <= 0)
    print_not_positive(d);
  else
    cout << "Box volume:  " << volume(h, w, d) << endl;
  return;
}

int get_positive(void)
{
  int temp;
  cout << "Enter a positive integer:  ";
  cin >> temp;
  return temp;
}

void print_prompt(void)
{
  cout << "You will now be prompted for box dimensions.\n";
  return;
}

void print_not_positive(int n)
{
  cout << "Error:  " << n << " is not positive.\n";
  return;
}