1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
The following is what I have but it isn't working right.
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
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 loop
while (userChoice != "Q" and userChoice != "q")
{
if (userChoice == "1")
{
doAverage();
}
else if (userChoice == "2")
{
doFactorial();
}
else if (userChoice == "3")
{
doMtoNth();
}
else if (userChoice == "4")
{
doRange();
}
userChoice = Menu();
}
return 0;
}
//string menu
string Menu()
{
string userChoice;
cout<< "Special Purpose Calculator"<<endl
<<endl
<< "1) Average"<<endl
<< "2) Factorial "<<endl
<< "3) M to the Nth Power"<<endl
<< "4) Range"<<endl
<<endl
<< "Q) Quit"<<endl
<<endl;
cout<< "Enter your choice? ";
cin >> userChoice;
return userChoice;
}
//int getValue
int getValue(string prompt, int lowBound, int upBound)
{
int number;
cout<< prompt<< lowBound<<" and "<<upBound<<": ";
do
{
if (number < lowBound or number > upBound)
{
cout<<endl<<"Error: "<<number<<" is invalid. Try again: ";
}
}
while (number < lowBound or number > upBound);
return number;
}
//void doAverage
void doAverage()
{
string state1="Enter a sum to be averaged between ";
string state2="Enter the number of entries ";
//declare variables
int lowBoundA = 1;
int upBoundA = 1000;
int lowBoundB = 1;
int upBoundB = 1000;
int sum;
int count;
double average;
sum=getValue(state1, lowBoundA, upBoundA);
count=getValue(state2, lowBoundB, upBoundB);
average=calcAverage(sum, count);
cout<<endl<<"The average is: ";
return;
}
//doulble calcAverage
double calcAverage( int sum, int count )
{
double average;
average=(double) sum/count;
return average;
}
//void doFactorial
void doFactorial()
{
string state1="Enter a number between: ";
//declare variables
int lowBound = 0;
int upBound = 10;
int numfactorial;
int factorial;
numfactorial=getValue(state1, lowBound, upBound);
factorial=calcFactorial(numfactorial);
return;
}
//int calcFactorial
int calcFactorial(int number)
{
int count,
factorial=1,
numfactorial;
count=numfactorial;
cout<<endl<<"The factorial value is: ";
while (count > 0)
{
factorial=factorial*count;
cout <<count<<endl<< "x";
count--;
}
cout<<" = "<<factorial;
return factorial;
}
//void doMtoNth
void doMtoNth()
{
string state1="Enter a value: ";
string state2="Enter a value: ";
//Declare variables
int lowBoundA=1;
int upBoundA = 10;
int lowBoundB = 1;
int upBoundB = 8;
int M;
int N;
int MtoNth;
double average;
M=getValue(state1, lowBoundA, upBoundA);
N=getValue(state2, lowBoundB, upBoundB);
MtoNth=calcMtoNth (M, N);
cout<<endl<<M<<" to the "<<N<< "th power is: ";
cout<<MtoNth;
return;
}
//int calcMtoNth
int calcMtoNth(int M, int N)
{
//Declare Variables
int MtoNth=1;
int count = 1;
int Nth;
while (count <= Nth)
{
MtoNth *=M;
count ++;
}
return MtoNth;
}
//void doRange
void doRange()
{
string state1="Enter a number between ";
string state2="Enter another number between ";
//Declare variables
int lowBound = 0;
int upBound = 1000;
int num1;
int num2;
int num3;
int num4;
int range=0;
double average;
num1=getValue(state1, lowBound, upBound);
num2=getValue(state2, lowBound, upBound);
num3=getValue(state2, lowBound, upBound);
num4=getValue(state2, lowBound, upBound);
range=calcRange(num1, num2, num3, num4);
cout<<endl<<"The range of " <<num1<<", "<<num2<<", "<<num3<<", "<<num4<<"is "<<range;
return;
}
//int calcRange
int calcRange(int number1, int number2, int number3, int number4)
{
//Declare variables
int max;
int min;
int range;
if (number1 >= number2)
max = number1;
else
max = number2;
if (number3 >= max)
max = number3;
if (number4 >= max)
max = number4;
if (number1 <= number2)
min = number1;
else
min = number2;
if (number3 <= min)
min = number3;
if (number4 <= min)
min = number4;
range = max - min;
return range;
}
|