1: /** @file count_digits.cpp
2: Counts the number of digits in a positive integer.
3: */
5: #include <iostream>
6: using namespace std;
8: #include "utilities.h"
9: using Scobey::Pause;
10: using Scobey::ReadInt;
11: using Scobey::userSaysYes;
13: int numberOfDigits
14: (
15: int n //in
16: )
17: /**<
18: Compute the number of digits in a positive integer.
19: @return The number of digits in the integer n.
20: @param n The integer whose digits are to be counted.
21: @post n has been initialized with a positive integer.
22: @post No other side effects.
23: */
24: {
25: if (n < 10)
26: return 1;
27: else
28: return 1 + numberOfDigits(n / 10);
29: }
31: int main()
32: {
33: cout << "\nThis program reads in a positive integer and then "
34: "computes\nand prints out the number of digits it contains.\n";
35: Pause();
37: do
38: {
39: int n;
40: ReadInt("Enter a positive integer: ", n);
41: cout << "The number of digits in " << n
42: << " is " << numberOfDigits(n) << ".\n\n";
43: }
44: while (userSaysYes("Do it again?"));
45: }