I think I figured everything out, so all I have to do now is make it look neater and add the comments back in:
#include <iostream>
#include <iomanip>
using namespace std;
string Menu();
int getValue( string, int, int );
void doAverage();
double calcAverage( int, int );
void doFactorial();
int calcFactorial( int );
void doMtoNth();
int calcMtoNth( int, int );
void doRange();
int calcRange( int, int, int, int );
int main()
{
string userChoice;
userChoice = Menu();
while ( userChoice != "Q" && userChoice != "q" )
{
if ( userChoice == "1" )
{
doAverage();
}
else if ( userChoice == "2" )
{
doFactorial();
}
else if ( userChoice == "3" )
{
doMtoNth();
}
else
{
doRange();
}
userChoice = Menu();
}
system("pause");
return 0;
}
string Menu()
{
cout << "\nSpecial Purpose Calculator"
<< "\n\n1) Average"
<< "\n2) Factorial"
<< "\n3) M to the Nth Power"
<< "\n4) Range"
<< "\n\nQ) Quit"
<< "\n\nEnter your choice: ";
string choice;
cin >> choice;
while ( (choice != "1") && (choice != "2") && (choice != "3") && (choice != "4") && (choice != "Q") && (choice != "q") )
{
cout << "Error: "
<< choice
<< " is invalid. Try again: ";
cin >> choice;
}
return choice;
}
int getValue( string prompt, int lowerBound, int upperBound)
{
int value;
cout << prompt
<< " between "
<< lowerBound
<< " and "
<< upperBound
<< ": ";
cin >> value;
while ( (value < lowerBound) || (value > upperBound) )
{
cout << "Error. "
<< prompt
<< " between "
<< lowerBound
<< " and "
<< upperBound
<< ": ";
cin >> value;
}
return value;
}
void doAverage()
{
int sum;
int count;
double average;
sum = getValue("Enter a sum", -1000, 1000);
count = getValue ("Enter a count", 0, 1000);
average = calcAverage(sum, count);
cout << "The average is: "
<< average
<< ".\n";
}
double calcAverage(int s, int c)
{
double average;
average = (s / c);
return average;
}
void doFactorial()
{
int facvalue;
int finvalue;
facvalue = getValue("Enter a value", 0, 10);
finvalue = calcFactorial(facvalue);
cout << "The factorial of "
<< facvalue
<< " is: "
<< finvalue
<< ".\n";
}
int calcFactorial(int f)
{
int i = 1;
int total = f;
for (i = 1; i < f; i++)
total *= i;
return total;
}
void doMtoNth()
{
int initm;
int initn;
int final;
initm = getValue("Enter the M value", 1, 10);
initn = getValue("Enter the N value", 1, 8);
final = calcMtoNth(initm, initn);
cout << initm
<< " raised to the "
<< initn
<< "th power is: "
<< final
<< ".\n";
}
int calcMtoNth(int m, int n)
{
int temp = 1;
int i = 0;
for(i = 0; i < n; i++)
temp *= m;
return temp;
}
void doRange()
{
int a, b, c, d, ranger;
a = getValue("Enter a number", 0, 10000);
b = getValue("Enter a number", 0, 10000);
c = getValue("Enter a number", 0, 10000);
d = getValue("Enter a number", 0, 10000);
ranger = calcRange(a, b, c, d);
cout << "The range of "
<< a
<< ", "
<< b
<< ", "
<< c
<< ", "
<< d
<< " is: "
<< ranger
<< ".\n";
}
int calcRange(int fi, int se, int th, int fo)
{
int min = fi, max = fi, range;
if (se <= min)
min = se;
if (th <= min)
min = th;
if (fo <= min)
min = fo;
if (se >= max)
max = se;
if (th >= max)
max = th;
if (fo >= max)
max = fo;
range = max - min;
return range;
}