Lottery game

This is a discussion on Lottery game within the C++ Programming forums, part of the General Programming Boards category; I'm creating a lottery game that solicits 6 numbers from the user between 1-59 and then generates 6 numbers until ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    25

    Lottery game

    I'm creating a lottery game that solicits 6 numbers from the user between 1-59 and then generates 6 numbers until they all match. When I compile and run my code it takes the user input fine. However as it runs through i want it to display what number its on and then keep going until it finds a match to the numbers. However when i do run it the code is just a line of long numbers and thats it. Any suggestions?

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <ctime> 
    #include <string>
    using namespace std;
    
    void getnums(int[]);
    
    int main() 
    { 
    	srand((unsigned) time(0));
    	const int MAXNUM = 6;
    	int count = 1;
    	int total = 0;
    	int x, n, nums[MAXNUM];
    	int lotnums[MAXNUM];
    	for(x=0;x<MAXNUM;x++)
    	{
    		cout << "\nPlease enter number " << x+1 << " : " ;
    		cin >> nums[x];
    		if ((nums[x] > 59) || (nums[x] < 1))
    		{
    			cout << "\nThat is not a valid entry." << endl;
    			x=x-1;	
    		}
    
    	}
    
    getnums(lotnums);
    
            for (x=0;x<MAXNUM;x++)
    			for (n=0;n<MAXNUM;n++)
    			if (lotnums[x]==nums[n])
    			{
    				cout << nums[0] << " " << nums[1] << " " << nums[2] << " " << nums[3] << " " << nums[4] << " " << nums[5] << endl << endl;
    				cout << lotnums[0] << " " << lotnums[1] << " " << lotnums[2] << " " << lotnums[3] << " " << lotnums[4] << " " << lotnums[5] << endl << endl;
    				cout << "/n/n Congratulations you just won the lotttery. It took " << total << " attempts." << endl << endl;
    			}
    			else if (lotnums[x]!=nums[n])
    			{
    				getnums(lotnums);
    				total+=count;
    				cout << total;
    			}
    				return 0;
    }
    
    
    
    void getnums(int lotnums[])
    {
    	lotnums[0] = 1 + rand() % 59;
    	do
    		lotnums[1] = 1 + rand() % 59;
    	while (lotnums[0] == lotnums[1]);
    	do
    		lotnums[2] = 1 + rand() % 59;
    	while ((lotnums[0] == lotnums[2]) || (lotnums[1] == lotnums[2]));
    	do
    		lotnums[3] = 1 + rand() % 59;
    	while ((lotnums[0] == lotnums[3]) || (lotnums[1] == lotnums[3]) || (lotnums[2] == lotnums[3]));
    	do
    		lotnums[4] = 1 + rand() % 59;
    	while ((lotnums[0] == lotnums[4]) || (lotnums[1] == lotnums[4]) || (lotnums[2] == lotnums[4]) || (lotnums[3] == lotnums[4]));
    	do
    		lotnums[5] = 1 + rand() % 59;
    	while ((lotnums[0] == lotnums[5]) || (lotnums[1] == lotnums[5]) || (lotnums[2] == lotnums[5]) || (lotnums[3] == lotnums[5]) || (lotnums[4] == lotnums[5]));
    
    	return;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your code prints out the lists everytime any number in the first set matches any number in the second set, not when all six are matched.

    Edit to add: if you're willing to deal with the STL, then std::set may be just the thing for you.
    Last edited by tabstop; 02-15-2008 at 09:26 AM.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    25
    ok then so how would i make it cout the count and then keep giong until all six numbers match?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So inside your loop all you do is check whether that particular number is a match, so you might do something like numMatched++ when you find a hit. (Make sure you reset numMatched to 0 before you start.)

    Then, after all your for loops have run, either numMatched==6, print and be done, or numMatched < 6 in which case you add 1 to total and do it again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-07-2010, 11:08 PM
  2. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  3. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  4. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21