The C++ Code:

Virginia Lottery Program:
 

/*Adam Clark
5-16-00
This program will search through lottery data to find the most commonly occuring numbers*/

//headers
#include <iostream.h>
#include <iomanip.h>
#include <ifstream.h>
#include <stdlib.h>
#include <conio.h>
#include <assert.h>
#include "bool.h"
#include "apstring.h"

//code for declarations

// Function : open_file
// Prompts user for the file name and opens
// input stream on the file
//
// Outputs: an input file stream

void open_file(ifstream &in_file);

//funtion: read_file
//
//this program will save the information in the file
//to an array of letters

void read_file(long lotto[ ]);

//Function:  selection
//organizes an array from
//
//input array
//output lotto array

void selection(long lotto[], int order[ ]);

//function: winning_numbers
//finds out if user has ever won the lottery
//
//inputs: six integers representing lottery numbers
//outputs: what you would have won

void winning_numbers(long lotto[ ],
  int user1, int user2, int user3, int user4, int user5, int user6);

int main()
{
 apstring answer;
 answer="y";
 while(answer=="y")
  {
  clrscr();
 //variables
 long total= 0;
 long lotto[45];
 int order[45];
 double percent = 0;
 double barfactor= 0;
 long biggest = 0;
 double barlengh;
 double denom;
 int a, b, c, d, e, f,choice;

 //formating
 cout< cout< //initialization
 for (int i = 0; i <= 44; i++)
 {
 lotto[i] = 0;
 order[i]= (i);
 }

 //opening
 cout<<"Adam's Great Lotto Database Program"< cout<<"This program searches the entire Virginia Lottery History"< cout<<"from Januaray 27, 1990 to May 13, 2000."< cout<<"What would you like to do?"< cout<< "1.) Display the top ten most frequently picked VA Lottery Numbers."< cout<< "2.) Display the ten least frequently picked VA Lottery Numbers."< cout<< "3.) Display the total number history of the VA Lottery."< cout<< "4.) Input 6 numbers to see what you would have ever won."< cout<<"?";
 cin>> choice;
 cout< if (choice==1)
  {
  read_file(lotto);
  //biggest_to_smallest(lotto,order);
  selection(lotto, order);

  for (int k = 0; k <= 44; k++)
   {
    total = total + lotto[k];
    if (lotto[k] > biggest)
    biggest = lotto[k];
   //cout<<(char)(order[k]+65)<   }

  //finds the bargraphfactor
  denom = ((double)biggest/(double)total)*100;
  barfactor = (50/ denom);

  //output loop for total distribution
 cout< for (int z = 44; z <35; z--)
  {
   percent = ((double)lotto[z]/(double)total) *100;
   barlengh = (int)(percent * barfactor);
   if (order[z]<=9)
    {
     cout<<(order[z])<<"    ";
    }
   else
    {
     cout<<(order[z])<<"   ";
    }
   for (int y = 1; y <= barlengh; ++y)
    {
     cout<<"*";
    }
   cout<<"   "<  }
 }
 
 

 if (choice==2)
  {
  read_file(lotto);
  //biggest_to_smallest(lotto,order);
  selection(lotto, order);

  for (int k = 0; k <= 44; k++)
   {
    total = total + lotto[k];
    if (lotto[k] > biggest)
    biggest = lotto[k];
   //cout<<(char)(order[k]+65)<   }

  //finds the bargraphfactor
  denom = ((double)biggest/(double)total)*100;
  barfactor = (50/ denom);

  //output loop for total distribution
 cout< for (int z = 1; z <=10; z++)
  {
   percent = ((double)lotto[z]/(double)total) *100;
   barlengh = (int)(percent * barfactor);
   if (order[z]<=9)
    {
     cout<<(order[z])<<"    ";
    }
   else
    {
     cout<<(order[z])<<"   ";
    }
   for (int y = 1; y <= barlengh; ++y)
    {
     cout<<"*";
    }
   cout<<"   "<  }
 }

 if (choice==3)
  {
  read_file(lotto);
  //biggest_to_smallest(lotto,order);
  selection(lotto, order);

  for (int k = 0; k <= 44; k++)
   {
    total = total + lotto[k];
    if (lotto[k] > biggest)
    biggest = lotto[k];
   //cout<<(char)(order[k]+65)<   }

  //finds the bargraphfactor
  denom = ((double)biggest/(double)total)*100;
  barfactor = (50/ denom);

  //output loop for total distribution
 for (int z = 1; z <=44; z++)
  {
   percent = ((double)lotto[z]/(double)total) *100;
   barlengh = (int)(percent * barfactor);
   if (order[z]<=9)
    {
     cout<<(order[z])<<"    ";
    }
   else
    {
     cout<<(order[z])<<"   ";
    }
   for (int y = 1; y <= barlengh; ++y)
    {
     cout<<"*";
    }
   cout<<"   "<  }
 }
 
 

 if (choice==4)
  {
  cout<<"What are your six numbers?"<  cout<<"1.)";
  cin>>a;
  cout<<"2.)";
  cin>>b;
  cout<<"3.)";
  cin>>c;
  cout<<"4.)";
  cin>>d;
  cout<<"5.)";
  cin>>e;
  cout<<"6.)";
  cin>>f;
  cout<  winning_numbers(lotto, a, b, c, d, e, f);
  }

 cout<< "Would you like to run this program again? [y/n] "< cin>> answer;
 }
    //closing
 cout<< endl<<"Thank you for using Melodious Groan Inc!!!"< return 0;
}
 

//subroutine to open file
void open_file(ifstream &in_file)
{
 apstring in_file_name;

 in_file_name = "a:/lotto.txt";       //file name
 in_file.open(in_file_name.c_str());    //opens file
 assert(! in_file.fail());              //bug-proofing
}

void read_file(long lotto[ ])
{
 int times_found, month, day, year, num1, num2, num3, num4, num5, num6;
 ifstream in_file;
 long total=0;
 apstring s;
 

 open_file(in_file);       //opens file

 while (! in_file.eof())     //does loop until the end of file
 {
  times_found= 0;  //initialization

  getline(in_file,s);
  month = atoi(s.substr(0,2).c_str());        //month
  day = atoi(s.substr(3,2).c_str());        //day
  year = atoi(s.substr(6,2).c_str());        //year

  num1 = atoi(s.substr(10,2).c_str());  //number 1
  num2 = atoi(s.substr(13,2).c_str());      //number 2
  num3 = atoi(s.substr(16,2).c_str());      //number 3
  num4 = atoi(s.substr(19,2).c_str());      //number 4
  num5 = atoi(s.substr(22,2).c_str());      //number 5
  num6 = atoi(s.substr(25,2).c_str());      //number 6
  ++total;

  //acumulator
  lotto[num1]++;
  lotto[num2]++;
  lotto[num3]++;
  lotto[num4]++;
  lotto[num5]++;
  lotto[num6]++;

 }

 cout<< "There where "< in_file.close();
}

void selection(long lotto[], int order[ ])
{
 int biggest = 0;
 int position = 0;
 int temp = 0;

 for (int i = 44;  i >= 1; i--)
  {
   biggest = 0;
   position = 0;
   for (int j = 0; j <= i; j++)
    {
     if (lotto[j] > biggest)
      {
       biggest = lotto[j];
       position = j;
      }
    }
   temp = lotto[position];
   lotto[position] = lotto[i];
   lotto[i]= temp;

   temp = order[position];
   order[position] = order[i];
   order[i]= temp;
  }
}

void winning_numbers(long lotto[ ],
  int user1, int user2, int user3, int user4, int user5, int user6)
{
 int times_found, month, day, year, num1, num2, num3, num4, num5, num6;
 int winnings = 0;
 int jackpot=0;
 ifstream in_file;
 long total=0;
 apstring s;
 

 open_file(in_file);       //opens file

 cout<<"These number occured on the following date(s): "< while (! in_file.eof())     //does loop until the end of file
 {
  times_found= 0;  //initialization

  getline(in_file,s);
  month = atoi(s.substr(0,2).c_str());        //month
  day = atoi(s.substr(3,2).c_str());        //day
  year = atoi(s.substr(6,2).c_str());        //year

  num1 = atoi(s.substr(10,2).c_str());  //number 1
  num2 = atoi(s.substr(13,2).c_str());      //number 2
  num3 = atoi(s.substr(16,2).c_str());      //number 3
  num4 = atoi(s.substr(19,2).c_str());      //number 4
  num5 = atoi(s.substr(22,2).c_str());      //number 5
  num6 = atoi(s.substr(25,2).c_str());      //number 6
  ++total;

  //acumulator
  lotto[num1]++;
  lotto[num2]++;
  lotto[num3]++;
  lotto[num4]++;
  lotto[num5]++;
  lotto[num6]++;
 

  if ((user1 == num1) || (user1 == num2) || (user1 == num3) || (user1 == num4)
   || (user1 == num5) || (user1 == num6))
    {
     times_found++;
    }
  if ((user2 == num1) || (user2 == num2) || (user2 == num3) || (user2 == num4)
   || (user2 == num5) || (user2 == num6))
    {
     times_found++;
    }
  if ((user3 == num1) || (user3 == num2) || (user3 == num3) || (user3 == num4)
   || (user3 == num5) || (user3 == num6))
    {
     times_found++;
    }
  if ((user4 == num1) || (user4 == num2) || (user4 == num3) || (user4 == num4)
   || (user4 == num5) || (user4 == num6))
    {
     times_found++;
    }
  if ((user5 == num1) || (user5 == num2) || (user5 == num3) || (user5 == num4)
   || (user5 == num5) || (user5 == num6))
    {
     times_found++;
    }
  if ((user6 == num1) || (user6 == num2) || (user6 == num3) || (user6 == num4)
   || (user6 == num5) || (user6 == num6))
    {
     times_found++;
    }

  switch (times_found)
   {
    case 3 :  cout<< "You picked three numbers on : "<< month<<"/"<       winnings = winnings + 1; break;
    case 4 :  cout<< "You picked four numbers on : "<< month<<"/"<       winnings = winnings + 50; break;
    case 5 :  cout<< "You picked five numbers on : "<< month<<"/"<       winnings = winnings + 1000; break;
    case 6 :  cout<< "You won the lotto on : "<< month<<"/"<       jackpot= jackpot+1;break;
   }

 }
 cout< cout< cout< in_file.close();
}

 
Back to My Main Lottery Page!