# help with a lottery problem.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-25-2010
todouble22
help with a lottery problem.
Hello all this is my first post and I have been lurking about since I have found this site. So here is my dilemma, I have a problem for school that will generate 3 random numbers between 0 and 9. GOT THAT I THINK.. it allows for users to pick 3 random numbers and compare them to the ones that have been generated. The awards are as follows: any one matching = \$10, two matching = \$100, three matching not in order = \$1000, and three matching in order = \$1,000,000. So I'm having an issue with my if statements so that they cover all the bases. I would love some feedback and what I can do to better the code that I have done since I am a newbie. should I make a struct for numbers like for guesse, generated, ? something along those lines?
Here is what I have:
Code:

```// lottery.cpp : main project file. #include "stdafx.h" #include <iostream> #include <ctime> using namespace std; int main () { // Declare variables                         int firstGuess,                 secondGuess,                 thirdGuess,                 firstResult,                 secondResult,                 thirdResult;                         cout << "This is a lottery game that allows the user to pick three numbers."  << endl;                 cout << "The game will randomly pick three numbers as well.."  << endl;                 cout << "The winnings will be based on the matching of the correct numbers picked." << endl;                 cout << "Any one matching = \$10, Any two matching = \$100," << endl;                 cout << "Any three matching, not in order = \$1,000." << endl;                 cout << "Three matching in exact order = \$1,000,000, No matches = \$0." << endl;                 cout << "Now the fun begins.. GOOD LUCK !!!" << endl;                 cout << "Please pick your first number: ";                 cin >> firstGuess;                 cout << "Please pick your second number: ";                 cin >> secondGuess;                 cout << "Pleas pick your third number: ";                 cin >> thirdGuess;                 cout << "Your picked numbers are: " << firstGuess << "--" << secondGuess <<"--" << thirdGuess << endl;                 cout << endl;                 cout << endl;                 cout << "And the winning numbers are.... " <<endl;         // Now draw the winning three numbers         const int DIVISOR = 10;         const int NUM = 3;                 srand ((unsigned) time (NULL));                 {         firstResult = rand() % DIVISOR;         secondResult = rand() % DIVISOR;         thirdResult = rand() % DIVISOR;         };         cout << firstResult <<"--" << secondResult <<"--" << thirdResult << endl;                 cout << endl; if (firstGuess == firstResult && secondGuess == secondResult && thirdGuess == thirdResult)         cout << "YOU WIN THE GRAND PRIZE!!!! \$1,000,000 " << endl; if (firstGuess == firstResult || firstGuess == secondResult || firstGuess == thirdResult         || secondGuess == firstResult || secondGuess == secondResult || secondGuess == thirdResult         || thirdGuess == firstResult || thirdGuess == secondResult || thirdGuess == thirdResult)         cout << "You win \$1,000 " << endl; if (firstGuess == firstResult || firstGuess == secondResult || firstGuess == thirdResult         && secondGuess == firstResult || secondGuess == secondResult || secondGuess == thirdResult         && thirdGuess == firstResult || thirdGuess == secondResult || thirdGuess == thirdResult)         cout << "You win \$100 " << endl;         return 0; }```
• 01-25-2010
rogster001
i have done quite a few 'spins' on lottery programs, i always used arrays, have never really done a class based version, , i dont know, will have a proper look in a bit...

by the way...what happens if the rand() call gets you two numbers the same?? thats not really fair on the player eh ;)
• 01-25-2010
Daved
I would make those if statements into if/else statements. A person who wins the \$1,000,000 prize technically qualifies for the lower prizes, too, but won't actually get them.

Also, your \$1000 and \$100 if statements aren't right. You'll have to think about how formulate them better. Start with the \$1000 part, as the logic is slightly simpler.

One option to make it easier would be to use functions if you've learned them. That could reduce the size of the confusing if statements.
• 01-25-2010
todouble22
Quote:

Originally Posted by rogster001
i have done quite a few 'spins' on lottery programs, i always used arrays, have never really done a class based version, , i dont know, will have a proper look in a bit...

by the way...what happens if the rand() call gets you two numbers the same?? thats not really fair on the player eh ;)

Funny that you mention that because that is another part of the problem that I need to add in? Like if the user picks 1.2.3 and the computer generates 1.1.1 i need to make it so that it doesnt give credit for three correct guesses. I'm just learning so i'm weeding my way through how to best do my if statements. not to mention i am taking the class online and the professor isnt much assistance at all.. dont rant hahah
• 01-25-2010
todouble22
Quote:

Originally Posted by Daved
I would make those if statements into if/else statements. A person who wins the \$1,000,000 prize technically qualifies for the lower prizes, too, but won't actually get them.

Also, your \$1000 and \$100 if statements aren't right. You'll have to think about how formulate them better. Start with the \$1000 part, as the logic is slightly simpler.

One option to make it easier would be to use functions if you've learned them. That could reduce the size of the confusing if statements.

could you just give me an example of the if else part of it? I am looking into functions now.. I appreciate all of you assistance..
• 01-25-2010
Daved
>> could you just give me an example of the if else part of it?
Code:

```if (...)     // do something else if (...)     // do something else if (...)     // do something```
It's just a matter of adding the else in front of the ifs (except the first one).
• 01-25-2010
todouble22
Quote:

Originally Posted by Daved
>> could you just give me an example of the if else part of it?
Code:

```if (...)     // do something else if (...)     // do something else if (...)     // do something```
It's just a matter of adding the else in front of the ifs (except the first one).

I got the idea of the if else statements. what about functions. my book is pretty vague. like a struct for numbers? i guess i'm a lil lost with that?
• 01-25-2010
Daved
Functions are a huge part of programming. It's important to learn them well and if your books explanation doesn't make a lot of sense then you should go elsewhere to find more information. I'd suggest waiting until your book introduces them before you start using them, though, assuming you haven't gotten to them in your book yet.

As far as a struct goes, you could do that. It wouldn't add too much to this program, though. The only reason I'd do it is if you specifically wanted to learn or practice structs. Otherwise, I'd concentrate on getting the program working correctly with your current setup.
• 01-25-2010
todouble22
• 01-26-2010
rogster001
Quote:

Funny that you mention that because that is another part of the problem that I need to add in? Like if the user picks 1.2.3 and the computer generates 1.1.1 i need to make it so that it doesnt give credit for three correct guesses.
You certainly want to fix that yes, unless the rules in your version of the game say that the same number can come out more than once in the draw
• 01-26-2010
todouble22
Quote:

Originally Posted by rogster001
You certainly want to fix that yes, unless the rules in your version of the game say that the same number can come out more than once in the draw

The number can come out more than once but my problem is doing the comparisons of all the possible drawn numbers to that of the guessed numbers. I'm having a real hard time with it. *banging head on desk**
• 01-26-2010
Daved
Your check for the \$1,000,000 is correct. Your check for the \$1000 is close but not quite right (forgetting about the possibility of duplicate numbers for the moment). See if you can work on that one. You have the right idea, but you aren't quite placing the ||'s and &&'s properly. Write out in English (using long winded this or this and that or that type words) what the conditions are for the \$1000 prize and then see if you can match it with code. Post your English sentence here.
• 01-26-2010
todouble22
the million dollar one is easy since they all have to be exactly equivalent in order.. so here goes: for the 1000 which is 3 matching not in order. first guess can == first result or second result or third result AND second guess can = first result or second result or third result AND third guess can equal first result or second result or third result?
• 01-26-2010
Daved
Looks good. Now here's the code in your first post that does that:
Code:

```if (firstGuess == firstResult || firstGuess == secondResult || firstGuess == thirdResult         || secondGuess == firstResult || secondGuess == secondResult || secondGuess == thirdResult         || thirdGuess == firstResult || thirdGuess == secondResult || thirdGuess == thirdResult)```
See anything that doesn't match up with the ORs and ANDs?
• 01-26-2010
todouble22
Quote:

Originally Posted by Daved
Looks good. Now here's the code in your first post that does that:
Code:

```if (firstGuess == firstResult || firstGuess == secondResult || firstGuess == thirdResult         || secondGuess == firstResult || secondGuess == secondResult || secondGuess == thirdResult         || thirdGuess == firstResult || thirdGuess == secondResult || thirdGuess == thirdResult)```
See anything that doesn't match up with the ORs and ANDs?

yes i do i have that in the part for the 100 i think?.. and firstly, thank you very much for your help! now i need to work it so that two matching will result in a win for \$100.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last