Setting limitations on variables

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;
}
After input is received from the user, use if statements to check if the input is not "safe". Either ask the user for more input, or just terminate the program with an error message. Whatever you deem more appropriate.
For your functions, use void instead since you're returning nothing.
I always find it useful to have a function to handle specialized inputs. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
int get_number( const char* message, int maximum )
{
	long long int result = 0;
	while (true)
	{
		cout << message << " (in 0.." << maximum << "): ";
		cin >> result;
		if (!cin) break;
		if ((result < 0) || (result > maximum)) continue;
		break;
	}
	return result;
}

Now you can say:

1
2
3
4
5
6
7
8
9
void exponent()
{
	int base;
	int power;
	do
	{
		base = get_number( "Enter the base of the number", 0x7FFFFFFF );
                if (!cin) break;


Your professor did not say how to prevent the user from computing a value greater than 7FFF,FFFF, but there are two general ways to do it:

Method 1) Don't let the user enter a 'power' that is too big (requires some math)

I prefer this method. To use it, you should #include <cmath> and use the logarithmic identity to compute the largest valid exponent.

    largest_valid_power = logbase( 7FFF,FFFF )

Remember that you can use any base with the simple identity:

                 loga( x )
    logb( x ) = -----------
                 loga( b )

You can put it all in a convenient function:

1
2
3
4
int max_power( int base )
{
	return log( 0x7FFFFFFF ) / log( base );
}

And now you can use it:

1
2
3
4
5
6
7
8
9
10
11
12
void exponent()
{
	int base;
	int power;
	do
	{
		base = get_number( "Enter the base of the number", 0x7FFFFFFF );
                if (!cin) break;

                power = get_number( "Enter the power", max_power( base ) );
                if (!cin) break;
                


Method 2) Watch for overflow when computing the answer (requires some special thinking)

If you prefer this instead, you'll have to adjust your answer loop to check for failure, and throw in a check for whether it succeeded or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int count;
for (count = 0; count < power; count++)
{
	if ((answer * base) < answer) break;
	else answer = answer * base;
}
if (count < power)
{
	cout << "Hey, " << base << " ** " << power << " makes a number that is too big!\n";
}
else
{
	cout << "The result is: " << answer << "\n";
}

I think this is quite a bit clunkier than the first solution, both in terms of how the program behaves for the user and in how the result is computed (and how easy it is to use).


A couple other things to note:

- Functions which do not return a value should not be returning a value:
  void welcome()

- The get_number() routine fails if you try to enter anything other than a number. Your welcome() function can mention that:
1
2
3
4
5
6
	cout << "      --------------------------\n";
	cout << "      - Exponential Calculator -\n";
	cout << "      --------------------------\n";
	cout << "\n";
	cout << "Enter 'quit' at any time to quit.\n";
	cout << "\n";

- Put that extra newline at the end of your main loop.

- You should put the code that computes the answer in a function. (This only really works nicely with option 1 above.)
1
2
3
4
5
6
7
int compute_power( int base, int power )
{
	int answer = 1;
	for (int count = 0; count < power; count++)
		answer = answer * base;
	return answer;
}
1
2
3
4
		cout << "The result is: " << compute_power( base, power ) << "\n";

		cout << "\n";
	} while (true);

- the exponent() function itself really is the main program. It could just go in main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
	welcome();

	do
	{
		int base = get_number( "Enter the base of the number", 0x7FFFFFFF );
		if (!cin) break;

		int power = get_number( "Enter the power", max_power( base ) );
		if (!cin) break;

		cout << "The result is: " << compute_power( base, power ) << "\n\n";

	} while (true);
}

Hope this helps.
Thank you so much! Duoas that was super helpful. It's also working perfectly, though it's a bit past my level at the moment, so I don't totally understand it as much as I'd like to. I get the main idea, but don't understand exactly what's going on in some functions. Thank you though! This helps satisfy the prompt and really has taught me a few lessons as well.
Last edited on
Topic archived. No new replies allowed.