A problem with this lottery chance calculator?

I'm not sure if I'm doing my calculations correct in this, and the book doesn't give an example run, so I was wondering if anyone would mind checking it and telling me if I've done this right, or what I did wrong. Here is the question and my attempt at it:

Question:
1
2
3
4
5
6
7
8
9
10
11
Many state lotteries use a variation of the simple lottery portrayed by Listing 7.4. In
these variations you choose several numbers from one set and call them the field numbers.
For example, you might select 5 numbers from the field of 1–47). You also pick a
single number (called a mega number or a power ball, etc.) from a second range, such as
1–27. To win the grand prize, you have to guess all the picks correctly. The chance of
winning is the product of the probability of picking all the field numbers times the probability
of picking the mega number. For instance, the probability of winning the example
described here is the product of the probability of picking 5 out of 47 correctly times the
probability of picking 1 out of 27 correctly. Modify Listing 7.4 to calculate the probability
of winning this kind of lottery.
 - taken from C++ Primer Plus 5th Ed. by Stephen Prata


and here is my attempt:
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
38
39
40
41
// lotto.cpp -- probability of winning

#include <iostream>
long double probability(unsigned numbers, unsigned picks);

int main()
{
	using namespace std;
	double total = 1, choices = 0, powerballRange;
	while (cin && choices <=total)
		{
		cout << "Enter the total number of choices on the game card and\n"
		"the number of picks allowed(q to quit):\n";
		cin >> total >> choices;
		if (!cin)
			break;
		cout << "Now enter the number of powerball choices: ";
		cin >> powerballRange;
		if (!cin)
			break;
		cout << "You have one chance in ";
		cout << (probability(total, choices)* (powerballRange/1)); // compute the odds
		cout << " of winning.\n";
		}
	cout << "bye\n";
	return 0;
}


// the following function calculates the probability of picking picks
// numbers correctly from numbers choices

long double probability(unsigned numbers, unsigned picks)
{
	long double result = 1.0; 
	long double n;
	unsigned p;
	for (n = numbers, p = picks; p > 0; n--, p--)
		result = result * n / p ;
	return result;
}

Last edited on
Try taking the "using namespace std;" out of the main function and placing it between "#include <iostream>" and the first mention of the probability function.
It does the same thing. I don't think that is a problem, the probability function doesn't use anything from std and it compiles fine. My problem is I don't know if it is giving me the correct answer or not when it calculates probability. I need an example run to compare it to or someone to check the math. (I hope I'm making sense)
The actual calculations are correct, so long as the user enters proper values.

For starters, total and choice should be unsigned, if they are doubles you can do silly things like have 2.3 choices or -3 picks. Doesn't make any sense. You would also need to make sure that the entered numbers are indeed positive. As you probably know, anything that is not a number ends the program (not just q, but things like *)

I really don't like your while loop, if cin is false, then you would have already broken from the loop, and you should probably break from the loop before you do the calculations if choices <= total anyways. Perhaps prompt the user to enter better numbers. Might I suggest a do while loop controlled by asking the user if they want another probability Y for yes N for no kinda deal. Or you could just have an infinite loop with the only exists being if they enter any none number and break that way.
Ah, okay, thank you for the help. I didn't think about the loop stuff you brought up, a do while would be a better fit than the one I have currently, think I'll change that and also make the doubles unsigned ints instead.
Topic archived. No new replies allowed.