Thread: help with a lottery problem.

  1. #1
    Registered User todouble22's Avatar
    Join Date
    Jan 2010
    Posts
    13

    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;
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    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
    Last edited by rogster001; 01-25-2010 at 10:54 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  4. #4
    Registered User todouble22's Avatar
    Join Date
    Jan 2010
    Posts
    13
    Quote Originally Posted by rogster001 View Post
    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

  5. #5
    Registered User todouble22's Avatar
    Join Date
    Jan 2010
    Posts
    13
    Quote Originally Posted by Daved View Post
    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..

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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).

  7. #7
    Registered User todouble22's Avatar
    Join Date
    Jan 2010
    Posts
    13
    Quote Originally Posted by Daved View Post
    >> 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?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  9. #9
    Registered User todouble22's Avatar
    Join Date
    Jan 2010
    Posts
    13
    Thanks for your help daved!

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    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

  11. #11
    Registered User todouble22's Avatar
    Join Date
    Jan 2010
    Posts
    13
    Quote Originally Posted by rogster001 View Post
    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**

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  13. #13
    Registered User todouble22's Avatar
    Join Date
    Jan 2010
    Posts
    13
    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?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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?

  15. #15
    Registered User todouble22's Avatar
    Join Date
    Jan 2010
    Posts
    13
    Quote Originally Posted by Daved View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Array Problem
    By Raekwon1 in forum C Programming
    Replies: 2
    Last Post: 04-13-2007, 04:59 PM