I'm having some trouble with setting limitations on variables.
We were given this assignment:
Design a program that:
Allows a user to input two numbers.
The user will be allowed to enter numbers of any value between 0 and 2,147,483,647 for either number.
The program is to calculate the result of raising the first number to the power of the second number (the second number is the exponent).
The program is to store the final result of the calculation in an Integer type variable.
The calculation is to be done using iteration (exponentiation is simply repetitious multiplication, just as multiplication is repetitious addition).
The program must implement safeguards to ensure that a number greater than 2,147,483,64710 is not placed in the Integer type variable that will hold the result of the calculation.
Once the calculation is performed, the program will display the result of the calculation (what is being held in the Integer type variable) or an appropriate error message.
An error message will be displayed if:
The user enters a number outside the allowed range of values given in condition 1.
The result of the calculation exceeds 2,147,483,647.
I think my code looks pretty good and satisfies all the criteria except it does not handle the safeguard aspect. What can I do to ensure no numbers exceed the values specified. Thank you.
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
|
#include<iostream>
using namespace std;
int welcome()
{
cout << " --------------------------\n";
cout << " - Exponential Calculator -\n";
cout << " --------------------------\n";
return 0;
}
int exponent()
{
int base;
int power;
do
{
int answer = 1;
cout << "\nEnter the base of the number (0 to exit): ";
cin >> base;
if (base == 0)
break;
cout << "Enter the power: ";
cin >> power;
for (int count = 0; count < power; count++)
answer = answer * base;
cout << "The result is: " << answer << endl;
} while (true);
return 0;
}
int main()
{
welcome();
exponent();
return 0;
}
|