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.