create a beginner c++ lottery ticket program using #include <iostream> ;<cstdlib
ID: 665074 • Letter: C
Question
create a beginner c++ lottery ticket program using #include <iostream> ;<cstdlib> ;<ctime>; <ctype>, bubble sort, sequencial search, functions, arrays, and comand line.
next to the sets of numbers should have a ALPHABET LETTER from A to J in each ticket depending of how many sets the user wants(1-10 sets).
the program must be called with a command line called "lotto" and the number of sets the user wants.
for example : lotto 43
output: 4 tickets of 10 sets and 1 ticket of 3 sets.
Explanation / Answer
Here is a lottery program which accepts any number :
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main()
{
srand(time(0));
int userinput[5], lotteryresults[5];
int counter=0;
char ans=0;
do{
//intro:
cout<<" ";
cout<<" Welcome to the lucky lottery! ";
cout<<" Let's get started.. ";
cout<<" Instruction: ";
cout<<" *Input five numbers between 0 and 9 only. ";
//inputnum:
for (int i=0; i<5; i++)
{cout<<" Please type your five lucky numbers for today (0-9): ";
cin>>userinput[i];
for(int a=1; a<=5; a++){
while(userinput[i]==userinput[i-a])
{cout<<" You've already entered that number. Please type another number: ";
cin>>userinput[i];}
}
while(userinput[i]<0||userinput[i]>10)
{cout<<" Enter numbers between 0-9 only. Please type another number: ";
cin>>userinput[i];}
}
for (int i=0; i<5; i++)
system("cls");
//results:
for (int x=0; x<40;x++){
cout<<" Generating numbers.... ";
cout<<" Please wait.... ";
for (int x=0;x<1;x++)
cout<<" ";};
system("cls");
cout<<endl;
cout<<" lottery WINNING NUMBERS: ";
for(int lot=0;lot<5; lot++){
lotteryresults[lot]= (rand()%10);
cout<<lotteryresults[lot]<<" ";
}
cout<<" USER NUMBERS: ";
for (int i=0; i<5; i++)
{cout<<userinput[i]<<" ";}
//matcher:
int matchnum[5];
for(int x = 0; x < 5; x++)
for(int y = 0; y < 5; y++)
if(userinput[x] == lotteryresults[y])
counter++;
int o=0;
for(int x = 0; x < 5; x++)
for(int y = 0; y < 5; y++){
if(userinput[x] == lotteryresults[y]){
matchnum[0] = userinput[x];
;o++;}
}
cout<<endl;
cout<<" MATCHING NUMBERS: ";
int x = 5;
for(int y=0; y<5;y++){
if(y>counter-1)
break;
x--;
cout<<matchnum[0]<<" ";}
cout<<" ";
cout<<endl;
if (counter<2)
cout<<" There are "<<counter<<" matching digit. .";
else
cout<<" There are "<<counter<<" matching digits. ";
cout<<" PRIZE: ";
switch(counter){
case 0:
cout<<"Sorry, none of your digits matched. ";
cout<<" Better luck next time! ";
break;
case 1:
cout<<"PHP10.00 (Balik-Taya) ";
break;
case 2:
cout<<"PHP500.00. ";
break;
case 3:
cout<<"PHP20,000. ";
break;
case 4:
cout<<"PHP100,000. ";
break;
case 5:
cout<<"All of your digits matched the lottery results. ";
cout<<" You won PHP500,000. ";
break;
}
if(counter!= 0)
for(int x = 0; x < 1; x++){
cout<<" CONGRATULATIONS!! ";
cout<<" ";
for(int y = 0; y < 1; y++)
;}
system ("pause");
system("cls");
cout<<endl<<" Do you want to play again [Y/N]? ";
cin>>ans;
while((ans != 'Y' && ans != 'y')&&(ans != 'N' && ans != 'n')){
cout<<endl<<" Please Enter Y or N only! ";
cout<<" Do you still want to play again?: ";
cin>>ans;
}
system("CLS");
}while(ans=='Y'||ans=='y');
cout<<" THANK YOU FOR PLAYING! ";
cout<<" Hope you enjoyed. ";
cout<<" Play again next time to win for more! ";
cin.sync();
cin.get();
return 0;
}
Another type of lottery.cpp :
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <set>
const size_t amount = 7;
typedef std::set<int> ticket_t;
ticket_t getlotteryPicks()
{
ticket_t ticket;
while (ticket.size() < amount)
{
std::cout << " Please enter number " << (ticket.size()+1) << ":";
int entry;
if(std::cin >> entry)
{
if(entry >= 1 && entry <= 40)
{
if (ticket.end() != ticket.find(entry))
std::cout << "Duplicate entry " << entry;
else
ticket.insert(entry);
} else
std::cout << "Entry out of range [1..40]: " << entry;
continue;
}
std::cin.clear();
std::string discard;
std::cin >> discard;
std::cout << "Bad input '" << discard << "' discarded";
}
return ticket;
}
ticket_t genWinNums()
{
ticket_t ticket;
while (ticket.size() < amount)
ticket.insert(rand() % 40 + 1);
return ticket;
}
std::ostream& operator<<(std::ostream& os, ticket_t const& t)
{
std::copy(t.begin(), t.end(), std::ostream_iterator<int>(os, "; "));
return os;
}
int main()
{
std::cout << "Lottery MODEL:" << std::endl;
std::cout << "---------------------------" << std::endl;
std::cout << " 1) Play lottery " << std::endl;
std::cout << " Q) Quit Program " << std::endl;
std::cout << "Please make a selection: ";
char choice;
std::cin >> choice;
switch(choice)
{
case '1':
{
std::cout << " Please enter your name: ";
std::string name;
std::cin >> name;
const ticket_t user = getlotteryPicks();
const ticket_t winning = genWinNums();
std::cout << "User ticket: " << user << " ";
std::cout << "Winning ticket: " << winning << " ";
// check win?
bool ok = 0 == std::lexicographical_compare(
user.begin(), user.end(),
winning.begin(), winning.end());
std::cout << "Result of draw: " << (ok? "WINNER":"No luck") << " ";
}
case 'Q': case 'q':
return 0;
default:
std::cout << " That is not a valid choice." << std::endl;
}
std::cin.ignore(1<<24,' ');
std::cout << " Press enter...";
std::string dummy;
std::getline(std::cin, dummy);
}
Hope it helps.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.