// inputFunctionDemo.cpp
//
// Demonstrates power function and an input function.

#include <iostream>

using namespace std;

double power(float base, int exponent);
bool inputNumbers(float& base, int& exponent);

/*
 * int main()
 *
 * Prompts user to enter base and exponent.
 * Outputs base, exponent, and power.
 *
 * Global variables:
 *    cin - standard input stream.
 *    cout - standard output stream.
 */
int main()
{
   // Announce purpose of program:
   cout << "This program will raise a floating-point "
             << "number to an integer power." << endl;

   // Input enteredBase and enteredExponent:
   float enteredBase;
   int enteredExponent;
   if ( ! inputNumbers(enteredBase, enteredExponent) )
      return 1;

   // The inputNumbers function, called above,
   // uses cin and cout.

   // Raise enteredBase to the enteredExponent power:
   double result = power(enteredBase, enteredExponent);

   // Display result:
   cout << enteredBase << " to the " << enteredExponent
               << " power is " << result << "." << endl;

   return 0;
}  // function main

/*
 * bool inputNumbers(float& base, int& exponent)
 *
 * Inputs a float base and an int exponent
 * typed by the user.  Prompts the user for
 * the inputs.  If either input is unsuccessful,
 * an error message is displayed.
 *
 * Parameters:
 *    base - a reference to the base,
 *           to be input from standard input
 *       Precondition: none
 *       Postcondition:  base contains a value
 *           entered by the user if input is successful,
 *           garbage otherwise.
 *    exponent - a reference to the exponent,
 *           to be input from standard input.
 *       Precondition: none
 *       Postcondition:  exponent contains a value
 *           entered by the user if input is successful,
 *           garbage otherwise.
 *
 * Global variables:
 *    cin - standard input stream.
 *       Precondition:  The state of cin is true.  (All
 *           previous interactive inputs were successful.)
 *       Postconditions:  The state of cin is true if bpth
 *           parameters were input successfully, false
 *           otherwise.  cin has NOT ended a line.
 *    cout - standard output stream
 *       Precondition:  cout is ready to begin a line.
 *       Postcondition:  If an error message was printed,
 *           cout has ended a line.  Otherwise, only
 *           prompts were printed, and cout has not
 *           ended a line.
 *
 * Returns:  true if both parameters were input
 *           sucessfully, false otherwise.
 */
bool inputNumbers(float& base, int& exponent)
{
   // Input base:
   cout << "Enter base (floating-point):>";
   cin >> base;

   // Check input error:
   if ( !cin )  {
      cout << "You did not enter a fixed-point number."
               << endl;
      return false;
   }  // if

   // Input exponent:
   cout << "Enter exponent (integer):>";
   cin >> exponent;

   // Check input error:
   if ( !cin )
      cout << "You did not enter an integer." << endl;

   return cin;
}  // function inputNumbers

/*
 * double power(float base, int exponent)
 *
 * Raises a floating-point number to a
 * specified integer power.
 *
 * No error-checking is done for out-of-range
 * values.
 *
 * Parameters:
 *    base - a number to be raised to a power.
 *       May be any float value such that, for
 *       a given exponent, the result will be
 *       within the double range without
 *       arithmetic overflow or underflow.
 *    exponent - an integer exponent.
 *       May be any int value such that, for
 *       a given base, the result will be
 *       within the double range without
 *       arithmetic overflow or underflow.
 *
 * Returns:
 *    base raised to the exponent power.
 */
double power(float base, int exponent)
{
   // If exponent is negative, do necessary conversions
   // to raise reciprocal of base to a positive power:
   if ( exponent < 0 )  {
      exponent = 0 - exponent;
      base = 1 / base;
   }  // if exponent < 0

   // Raise base to a non-negative power:
   double product = 1;
   for ( int i = 0; i < exponent; i++ )
      product = product * base;
   return product;
}  // function power