Thread: lottery program mega million

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    lottery program mega million

    hi guys .
    im new to c++
    im actually working on a simulation to the mega million lottery.
    but i have a problem matching the user's number and the lottery number (random number).

    that my program :
    Code:
    #include <iostream>
    #include<iomanip>
    #include <ctime>
    #include<string>
    #include<fstream>
    using namespace std;
    void winningnum();
    void userchoice();
    void getexit();
    int play, draw;
    const int size = 5;
    const int size2 = 1;
    int MBnumber[size2];
    int lottery[size];
    int user[size];
    int jackpot= 300000;
    bool sendback=true;
    bool searchmyarray(int userval , int indexval);
    bool SearchLottreMyyArray(int userValue,int indexValue);
    void mactchingnumbers();
    
    int main (){
    	char again;
    
    	do{
    		userchoice();
    		winningnum();
    		mactchingnumbers();
    
    
    
    		cout <<"\n would you like to play again :";
    		cin >> again;
    
    	} while( again == 'y' || again == 'Y' );
    	getexit();
    	return 0;
    
    }
    }
    void userchoice(){
    
    	cout <<"how many play , would you like to play(choose from 1 to 3 ): " ;
    	cin >> play;
    	while (play >3 || play < 1){
    		cout <<"\nentry not valid , please choose a numbe in the range of 1 to 3: ";
    		cin >> play;
    	}
    
    	cout <<"\nhow many draws would you like to play "
    		"\nfor you picks(choose from day 1 to day 7 ) :";
    	cin>> draw;
    	while ( draw > 7 || draw < 1 ){
    		cout<<"\nplease choose a number from 1 to 7 :" ;
    		cin >> draw;
    	}
    	cout <<"\ngood luck , so you are going to play with "<<play<<"  group of number for "<<draw<<"  days"<<endl;
    	cout <<"Enter you 5 picks in the range of 1 to 56"<<endl;
    
    	ifstream inputfile;
    	inputfile.open("usersnumbers.txt");
    	for (int k =0 ;k < play; k++){
    		cout <<"Numbers for play number "<<k+1<<" :";
    		for(int i =0 ;i <size; i++){
    			//cout<<"Number " << i+1 << " : ";
    			//cin >>user[i];
    			inputfile>>user[i];
    			cout<<"["<<user[i]<<"]";
    			while(sendback){
    				while (user[i]> 56 || user[i]< 1){
    					cout<<"please choose a number in the range of 1 to 56 :";
    					cin>>user[i];
    					cin.ignore();
    					cin.clear();
    				}
    				if(user[i] != 0 )
    					sendback= searchmyarray(user[i],i);
    				else 
    					sendback= false;
    			}sendback = true;
    
    			//cout<<endl;
    		}		
    		cout <<"\nMegaBall Number for play "<<k+1<<" : ";
    		for(int j = 0; j < size2 ;  j++){
    			//cin>> MBnumber[j];
    			inputfile>>MBnumber[j];
    			cout<<"["<<MBnumber[j]<<"]"<<endl;
    			while ( MBnumber[j] > 46 || MBnumber[j] < 1){
    				cout <<"\nplease choose a number in the range of 1 to 46 : ";
    				cin>>MBnumber[j];
    			}
    			cout<<endl;			
    		}
    	}
    	inputfile.close();
    }
    void winningnum(){
    
    	srand(time(0));
    
    	cout <<"\n And the winning numbers are :\t"<<endl;
    	ifstream file;
    	file.open("randomnumber.txt");
    	for (int k =0 ;k <draw; k++){
    		cout<<"\nNumbers for play number "<<k+1<<" : ";
    		for(int w =0 ;w <size; w++){
    			while(sendback){
    				if(lottery[w]!=0){
    					sendback= SearchLottreMyyArray(lottery[w],w);
    				}
    				else {
    					sendback= false;
    				}
    			}sendback = true;
    
    			//int num2= rand () % 56 + 1;
    			//lottery[w] = num2;
    			//cout <<"["<< num2 <<"]";
    			file>>lottery[w];
    			cout<<"["<<lottery[w]<<"]";
    		}		
    		cout <<endl;
    		cout <<"The megaball number : \t";
    
    		for(int j = 0; j < size2 ;  j++){
    			//lottery[j] =  1+ rand () % 46;
    			//cout <<"["<<lottery[j]<<"]";
    			file>>lottery[j];
    			cout<<"["<<lottery[j]<<"]";
    		}
    		cout<<endl;
    	}
    	file.close();
    }
    bool searchmyarray(int x,int y){
    	bool status= true;
    	for(int i =0 ; i < y ; i++)
    
    		if(x ==  user[i]){
    			cout<<"\n you choose a duplicate number, choose another one.";
    			cin>> user[i];
    			status= false;
    		}
    		else {
    			status = false;
    		}
    		status=false;
    
    		return status;
    }
    bool SearchLottreMyyArray(int x,int y){
    
    	bool status= true;
    	for(int w =0 ; w < y ; w++){
    		//	int num1 = rand () %  56 +1 ;
    		if(x ==  lottery[w]) {
    			cout <<"["<< lottery[w] <<"]";
    			status = false;
    		}
    		else {
    			status = false;
    		}
    	}
    	status=false;
    
    	return status;
    }
    void mactchingnumbers(){
    	int NumMatch = 0 ;
    	int MBmatch=0;
    	cout <<"\nRESULT : "<<endl;
    	for(int  p = 0 ; p < play ; p++ ){	
    		cout <<"Play #"<<p+1<<" Winnings :  ";
    		for(int  d=0 ; d<draw ; d++ ){
    			cout<<"Draw # "<< d+1<<" " <<endl;
    
    
    			for(int i =0; i <size; i++){
    				//int temp = user[i];
    				for(int w =0 ; w<size;w++){
    					if (user[i] == lottery[w]){
    						cout<<user[i]<<endl;
    						NumMatch++;
    
    					}
    				}
    			}
    			cout<<"you have " <<NumMatch<<" out of five ";
    			cout<<endl;}
    
    
    	}
    }
    if you can help me to find the function that will allow me to macth the user number and lottery number (for each play and draw .)

    thank you.
    Attached Files Attached Files

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    std::equal() would solve your problem.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    actually i dont konw how to use it
    because i tried many things and still didnt work

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What is the point of the return value of SearchLottreMyyArray? It always returns false. Are you expecting it to somehow return true? (On the other hand, you seem to not really be using it, so oh well.)

    You overwrite the first number drawn by the lottery with the MB number, so that data is lost. There's nothing wrong with the way you check for matching numbers.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    3
    hello tabstop.
    thank you for your answer.
    actually i have a problem with the matching function
    because the goal is to match user array and the lottery array for each play(1 to 3) and for each draw (1 to 7).
    so when i run the program and enter like 1 play and 1 draw , the matching is correct, but when i put more than one play or draw everything gets messed up.


    thank you.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nowhere in your code do you get more than one day's worth of numbers. Putting in a loop for d going from 0 to draw is nice, but if you don't get new numbers inside that loop it doesn't help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A lottery program - arrays, ints, chars etc
    By Glauber in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2008, 10:48 AM
  2. mega compile errors for small program
    By s_UNI_ in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2005, 12:00 PM
  3. Yet another lottery program
    By BungleSpice in forum C Programming
    Replies: 10
    Last Post: 04-01-2004, 02:08 PM
  4. FINISHED - lottery program
    By threahdead in forum C Programming
    Replies: 7
    Last Post: 01-08-2003, 09:28 AM