SOURCE FILE: 030_powCalcA_2.0.cpp
/*
Here we add the rest of the function definitions.
This is the level 2.0 program. Since there are no
level 3 functions, this is also the final level -
the finished program.
*/
#include <iostream>
using namespace std ;
/* PROTOTYPES */
void PrintInfo() ;
int GetInt() ;
bool GoodInput(int first, int second) ;
int MyPow(int base, int exponent) ;
void WriteAnswer (int base, int exp, int ans) ;
/* ************** */
/* FUNCTION: MAIN */
/* ************** */
int main (void)
{
int n, m, answer ;
/* Print info on what the program does */
PrintInfo() ;
do
{
/* get the two inputs (n and m) */
n = GetInt() ;
m = GetInt() ;
/* if n and m are both positive */
if ( GoodInput(n,m) )
{
/* compute n to the power m */
answer = MyPow (n,m) ;
/* write the answer on the screen */
WriteAnswer (n,m,answer) ;
}
} while (GoodInput(n,m)) ; /* while n and m are both positive */
cout << endl ;
return 0;
}
/* FUNCTION: PRINT INFO */
void PrintInfo()
{
cout << "\n" ;
cout << " This program inputs two positive integers n and m. \n" ;
cout << " It calculates and displays n to the power m. \n" ;
cout << " For example if you enter 2 and 3, the program calculates\n" ;
cout << " 2*2*2 = 8. \n" ;
cout << "\n" ;
cout << " The program repeats until the user enters a non-positive\n" ;
cout << " number for one or both of the inputs.\n" ;
cout << "\n" ;
}
/* FUNCTION: GET INT */
int GetInt()
{
int entry ;
cout << "Please enter an integer (<=0 to stop): " ;
cin >> entry ;
return entry ;
}
/* FUNCTION: GOOD INPUT */
bool GoodInput(int first, int second)
{
return ( (first > 0) && (second > 0) ) ;
}
/* FUNCTION: MY POW */
int MyPow(int base, int exponent)
{
int j, result = 1 ;
for (j=1; j<=exponent; j++)
{
result = result * base ;
}
return result ;
}
/* FUNCTION: WRITE ANSWER */
void WriteAnswer (int base, int exp, int ans)
{
cout << endl ;
cout << "The value of " << base << " to the power " << exp
<< " is: " << ans << endl ;
cout << endl ;
}