Sams Teach Yourself C++ in 21 Days

(singke) #1
12: cout << “Enter two numbers.\n Number one: “;
13: cin >> one;
14: cout << “Number two: “;
15: cin >> two;
16: answer = Divider(one, two);
17: if (answer > -1)
18: cout << “Answer: “ << answer;
19: else
20: cout << “Error, can’t divide by zero!”;
21: return 0;
22: }
23:
24: short int Divider(unsigned short int valOne, unsigned short int
➥valTwo)
25: {
26: if (valTwo == 0)
27: return -1;
28: else
29: return valOne / valTwo;
30: }


  1. The following is one possible solution:
    1: #include
    2: using namespace std;
    3: typedef unsigned short USHORT;
    4: typedef unsigned long ULONG;
    5:
    6: ULONG GetPower(USHORT n, USHORT power);
    7:
    8: int main()
    9: {
    10: USHORT number, power;
    11: ULONG answer;
    12: cout << “Enter a number: “;
    13: cin >> number;
    14: cout << “To what power? “;
    15: cin >> power;
    16: answer = GetPower(number,power);
    17: cout << number << “ to the “ << power << “th power is “ <<
    18: answer << endl;
    19: return 0;
    20: }
    21:
    22: ULONG GetPower(USHORT n, USHORT power)
    23: {
    24: if(power == 1)
    25: return n;
    26: else
    27: return (n * GetPower(n,power-1));
    28: }


828 Appendix D

32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 828

Free download pdf