Case Study: Revising the Lottery Program Using Strings in C++

A problem can be solved using many different approaches. This section rewrites the Point lottery program in Listing 3.7, Lottery.cpp, using strings. Using strings simplifies this program.

The lottery program in Listing 3.7 generates a random two-digit number, prompts the user to enter a two-digit number, and determines whether the user wins according to the following rules:

  1. If the user input matches the lottery number in the exact order, the award is $10,000.
  2. If all the digits in the user input match all the digits in the lottery number, the award is $3,000.
  3. If one digit in the user input matches a digit in the lottery number, the award is $1,000.

The program in Listing 3.7 uses an integer to store the number. Listing 4.8 gives a new program that generates a random two-digit string instead of a number and receives the user input as a string instead of a number.

Listing 4.8  LotteryUsingStrings.cpp

1 #include <iostream>

2 #include <string> // for using strings

3 #include <ctime> // for time function

4 #include <cstdlib> // for rand and srand functions

5 using namespace std;

6

7 int main()

8 {

9     string lottery;

10    srand(time(0));

11    int digit = rand() % 10; // Generate first digit

12    lottery += static_cast<char>(digit + ‘0’);

13    digit = rand() % 10; // Generate second digit

14    lottery += static_cast<char>(digit + ‘0’);

15

16    // Prompt the user to enter a guess

17    cout << “Enter your lottery pick (two digits): “;

18    string guess;

19    cin >> guess;

20

21    cout << “The lottery number is ” << lottery << endl;

22

23    // Check the guess

24    if (guess == lottery)

25    cout << “Exact match: you win $10,000” << endl;

26    else if (guess[1] == lottery[0] && guess[0] == lottery[1])

27    cout << “Match all digits: you win $3,000” << endl;

28    else if (guess[0] == lottery[0] || guess[0] == lottery[1]

29          || guess[1] == lottery[0] || guess[1] == lottery[1])

30    cout << “Match one digit: you win $1,000” << endl;

31    else

32    cout << “Sorry, no match” << endl;

33

34    return 0;

35 }

The program generates the first random digit (line 11), casts it to a character, and concatenates the character to the string lottery (line 12). The program then generates the second random digit (line 13), casts it to a character, and concatenates the character to the string lottery (line 14). After this, lottery contains two random digits.

The program prompts the user to enter a guess as a two-digit string (line 19) and checks the guess against the lottery number in this order:

  1. First, check whether the guess matches the lottery exactly (line 24).
  2. If not, check whether the reversal of the guess matches the lottery (line 26).
  3. If not, check whether one digit is in the lottery (lines 28-29).
  4. If not, nothing matches and display “Sorry, no match” (lines 31-32).

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *