Source Code for the VA Lottery Program

//This program was created by Tiffany Overstreet and Melissa Skaar for AP Computer Science.
//It was edited on May 15, 2000 to have a continuous run function and
//menu selection by Tiffany Overstreet.

#include <iostream.h>
#include <fstream.h>
#include "apstring.h"
#include <assert.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>


int get_user_numbers(int user_ticket[]);
//This function will get the user's numbers and put them into an array.

int open_input_file(ifstream &in_file, apstring name);
//This function will open the file.

int load_file_into_array(ifstream &in_file, int &month, int &day, int &year, int occurence[], int &ct, apstring &name, char &kicker, int user_ticket[], int lotto_ticket[], int &total_money_spent, int &total_money_won, int &jackpot, int &grand_total);
//This function will load the data into the separate arrays.

int compare_numbers(int data, int user_ticket[], int &tick_mark);
//This function will compare the user's numbers to the numbers that did occur.

int print_results_of_comparison(int &tick_mark, int &month, int &day, int &year, int lotto_ticket[], int &total_money_spent, int &total_money_won, int &jackpot);
//This function will output what the user won and when.

int total_spent(int &total_money_spent, int &total_money_won, int &jackpot);
//This function will print out the total money spend and won by the user.

out_put(int &grand_total, int occurence[], int number[]);
//This function will output the bar graphs.

int selection_sort(int occurence[], int number[], int &option);
//This function will put the numbers in order from greatest to least.

int main()
{
ifstream in_file;
int occurence[44];
int number[44];
int user_ticket[6];
int lotto_ticket[6];
apstring name;
char kicker;
int month, day, year;
int option;
char choice;

cout<<"Welcome to the menu driven version of the lotto program written by"<<endl;
cout<<"Tiffany Overstreet and Melissa Skaar. This program was edited and"<<endl;
cout<<"created by Tiffany Overstreet on May 15, 2000."<<endl<<endl;

do
{
int ct=0;
int total_money_spent=0;
int total_money_won=0;
int jackpot=0;
int grand_total=0;


for (int x=0; x<=43; x++)
{
occurence[x]=0;
number[x]=x+1;
}

for (int y=0; y<=5; y++)
{
user_ticket[y]=0;
lotto_ticket[y]=0;
}


cout<<"Here are your options:"<<endl;
cout<<setw(5)<<"Option #1: View the ten most frequently occuring numbers."<<endl;
cout<<setw(5)<<"Option #2: View the ten least frequently occuring numbers."<<endl;
cout<<setw(5)<<"Option #3: View a bar graph of the distribution of all lottery numbers."<<endl;
cout<<setw(5)<<"Option #4: Play the lottery for the last ten years and see if you won!"<<endl;
cout<<endl<<"What option number would you like? ";
cin>>option;
while (option<1 || option>4)
{
cout<<"You did not enter a valid option. Please enter a valid option: ";
cin>>option;
}

//This is option one or two.
if (option==1 || option==2)
{
name="a:\data.txt";
kicker='n';
load_file_into_array(in_file, month, day, year, occurence, ct, name, kicker, user_ticket, lotto_ticket, total_money_spent, total_money_won, jackpot, grand_total);

name="a:\kicker.txt";
kicker='y';
load_file_into_array(in_file, month, day, year,occurence, ct, name, kicker, user_ticket, lotto_ticket, total_money_spent, total_money_won, jackpot, grand_total);

selection_sort(occurence, number, option);
cout<<endl;
}

//This is option three.
if (option==3)
{
name="a:\data.txt";
kicker='n';
load_file_into_array(in_file, month, day, year, occurence, ct, name, kicker, user_ticket, lotto_ticket, total_money_spent, total_money_won, jackpot, grand_total);

name="a:\kicker.txt";
kicker='y';
load_file_into_array(in_file, month, day, year,occurence, ct, name, kicker, user_ticket, lotto_ticket, total_money_spent, total_money_won, jackpot, grand_total);

selection_sort(occurence, number, option);
out_put(grand_total, occurence, number);
}


//This is option four.
if (option==4)
{
get_user_numbers(user_ticket);

name="a:\data.txt";
kicker='n';
load_file_into_array(in_file, month, day, year, occurence, ct, name, kicker, user_ticket, lotto_ticket, total_money_spent, total_money_won, jackpot, grand_total);

name="a:\kicker.txt";
kicker='y';
load_file_into_array(in_file, month, day, year,occurence, ct, name, kicker, user_ticket, lotto_ticket, total_money_spent, total_money_won, jackpot, grand_total);

total_spent(total_money_spent, total_money_won, jackpot);
cout<<endl;
}


cout<<"Would you like to run the program again? (y/n) ";
cin>>choice;
cout<<endl;
}
while (choice!='n');

cout<<"Bye-bye! Thanks for playing!";



return 0;
}


int get_user_numbers(int user_ticket[])
{
int number=0;
cout<<"Please enter in your six numbers: "<<endl;

for (int x=0; x<=5; x++)
{
cout<<"Number "<<x+1<<": ";
cin>>number;
if (number<=44 && number>=1)
{
user_ticket[x]=number;
}
else
{
cout<<"You have entered an invalid number. Please remember that"<<endl;
cout<<"in the lotto pick six you can only enter numbers from one"<<endl;
cout<<"to forty-four. Please enter another number for this slot."<<endl;
x=x-1;
}
}
cout<<endl;
return 0;
}

open_input_file(ifstream &in_file, apstring name)
{
apstring in_file_name;
in_file_name=name;
in_file.open(in_file_name.c_str());
assert(! in_file.fail());

return 0;
}

int load_file_into_array(ifstream &in_file, int &month, int &day, int &year, int occurence[], int &ct, apstring &name, char &kicker, int user_ticket[], int lotto_ticket[], int &total_money_spent, int &total_money_won, int &jackpot, int &grand_total)
{
int data;
int value;
int tick_mark; //This will keep track of how many numbers match up from the user.


open_input_file(in_file, name);
//cout<<endl;
//cout<<"I opened the file: "<<name<<endl;

while(! in_file.eof())
{
//cout<<"ct= "<<ct<<endl;
//This stores the month, day, and year in variables.
tick_mark=0;

in_file>>(month);
in_file>>(day);
in_file>>(year);

total_money_spent=total_money_spent+1;

if (kicker=='n')
value=5;

if (kicker=='y')
value=11;

for (int x=0; x<=value; x++)
{
if(x>=0 && x<=5)
{
in_file>>(data);
grand_total=grand_total+data;
lotto_ticket[x]=data;
occurence[data-1]=occurence[data-1]+1;
compare_numbers(data, user_ticket, tick_mark);

//cout<<data<<" "<<occurence[data-1]<<endl;
}
else
in_file>>(data);
}
//cout<<endl;

if (tick_mark>=3)
{
print_results_of_comparison(tick_mark, month, day, year, lotto_ticket, total_money_spent, total_money_won, jackpot);
}

ct=ct+1;
getline(in_file, name); //Please note that name is a dummy file marker.
//It is used only to check for the end of file at the end of a line.
}


in_file.close();


return 0;
}

int compare_numbers(int data, int user_ticket[], int &tick_mark)
{
for (int x=0; x<=5; x++)
{
if (data==user_ticket[x])
{
tick_mark=tick_mark+1;
}
}

return 0;
}

int print_results_of_comparison(int &tick_mark, int &month, int &day, int &year, int lotto_ticket[], int &total_money_spent, int &total_money_won, int &jackpot)
{



if (tick_mark==3)
{
cout<<"You had three matching numbers on the date "<<month<<"/"<<day<<"/"<<year<<"."<<endl;
cout<<" The ticket for that date was: ";
for (int a=0; a<=5; a++)
{
cout<<lotto_ticket[a]<<" ";
}
cout<<"-- You won $1.00!"<<endl<<endl;
total_money_won=total_money_won+1;
}

if (tick_mark==4)
{
cout<<"You had four matching numbers on the date "<<month<<"/"<<day<<"/"<<year<<"."<<endl;
cout<<" The ticket for that date was: ";
for (int b=0; b<=5; b++)
{
cout<<lotto_ticket[b]<<" ";
}
cout<<"-- You won $50.00!"<<endl<<endl;
total_money_won=total_money_won+50;
}

if (tick_mark==5)
{
cout<<"You had five matching numbers on the date "<<month<<"/"<<day<<"/"<<year<<"."<<endl;
cout<<" The ticket for that date was: ";
for (int c=0; c<=5; c++)
{
cout<<lotto_ticket[c]<<" ";
}
cout<<"-- You won $1,000.00!"<<endl<<endl;
total_money_won=total_money_won+1000;
}

if (tick_mark==6)
{
cout<<"************ :-) ************** :-) ************"<<endl;
cout<<" You had six matching numbers on the date "<<month<<"/"<<day<<"/"<<year<<"."<<endl;
cout<<" You won the jackpot! :-)"<<endl;
cout<<"************ :-) ************** :-) ************"<<endl<<endl;
jackpot=jackpot+1;
}


return 0;
}

int total_spent(int &total_money_spent, int &total_money_won, int &jackpot)
{

cout<<endl;
cout<<"If you played this number every drawing since the lottery began on 1/27/90,"<<endl;
cout<<"you would have spent $"<<total_money_spent<<".00"<<endl;
cout<<"If you played this number every drawing since the lottery began on 1/27/90,"<<endl;
cout<<"you would have won $"<<total_money_won<<".00"<<endl;

if (jackpot>=1)
{
cout<<"You won the jackpot "<<jackpot<<" times! Congratulations!"<<endl;
}
else
{
cout<<"You haven't won the jackpot yet."<<endl;
}

return 0;
}

selection_sort(int occurence[], int number[], int &option)
{
int temporary, greatest, position, temp_num;


for (int i=43; i>=0; i--)
{
greatest=0;

for (int ct=0; ct<=i; ct++)
{
if (greatest<occurence[ct])
{
greatest=occurence[ct];
position=ct;
}
}
//exchange of the numbers
temporary=occurence[i];
occurence[i]=greatest;
occurence[position]=temporary;

//exchange the number position
temp_num=number[i];
number[i]=number[position];
number[position]=temp_num;

}

if (option==1)
{
cout<<"The ten most frequently occuring numbers are:"<<endl;
for (int b=43; b>=34; b--)
{
cout<<number[b]<<" with "<<occurence[b]<<" number of occurences."<<endl;
}
}

if (option==2)
{
cout<<"The ten least frequently occuring numbers are:"<<endl;
for (int a=0; a<=9; a++)
{
cout<<number[a]<<" with "<<occurence[a]<<" number of occurences."<<endl;
}
}

return 0;
}

out_put(int &grand_total, int occurence[], int number[])
{
int largest=0;
double scale_factor;
int scale;

largest=occurence[43];
//This will determine the scale factor
scale_factor=(50/(double)largest);

//This loop goes through each slot of the array and determines percentages.
for (int n=0; n<=43; n++)
{
scale=(occurence[n])*scale_factor;

cout<<number[n]<<" : ";


for (int i=1; i<=scale; i++)
{
cout<<"*";
}
cout<<" "<<occurence[n]<<" occurences"<<endl;

}

return 0;
}

Main Lottery Page    Sample Output