Problem No#15 of Chapter 7 "Arrays" out of the book of "Getting Started with C++" by Tony Gaddis
/*
Name: Asad Ali
Copyright: All Rights Reserved by JanGoO Group of Companies
Author: Tony Gaddies
Date: 15/01/19 00:12
Description: This program basically comparies user input
lottery number to the computer generated lottery ticket
number after comparing each digit, it show out put of
number of digits same in computer generated lottery
number and user entered lottery number.
Then there are some pre-fiexed prizes in the program:
If
1. One digit = prize of Rs.100
2. Two digits = prize of Rs.200
3. Three digits = prize of Rs.300
4. Four digits = prize of Rs. 400
5. Five digits = prize of Rs. 500
and program will output one of these prizes accourding to
the number of digits matched.
//// Computer Lottery Digits <compure with> User Lottery Digits
*/
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
const int SIZE = 5;
int lottery_arr[SIZE];
int user_arr[SIZE];
int num_of_same_digits = 0;
srand(time(NULL));
for ( int i = 0; i < 5; i++ )
{
lottery_arr[i] = rand() % 10;
}
cout << ".-----------------------------------------------.\n";
cout << "|\t \t \t \t \t \t|" << endl;
cout << "| Ticket has 5 digits for example : ";
for ( int i = 0; i < 5; i++ )
{
cout << lottery_arr[i] << " ";
}
cout << " |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "|-----------------------------------------------|";
cout << endl;
cout << "| Enter your lottery ticket digits one by one |" << endl;
cout << "'-----------------------------------------------'\nINPUT\\>\n";
for ( int i = 0; i < 5; i++ )
{
cin >> user_arr[i];
}
cout << endl;
cout << ".-----------------------------------------------.\n";
for ( int i = 0; i < 5; i++ )
{
if ( lottery_arr[i] == user_arr[i] )
{
num_of_same_digits = num_of_same_digits + 1;
}
}
cout << "| Winner Lottery ticket digits are : ";
for ( int i = 0; i < 5; i++ )
{
cout << lottery_arr[i] << " ";
}
cout << "|" << endl;
cout << "| User Lottery ticket digits a r e : ";
for ( int i = 0; i < 5; i++ )
{
cout << user_arr[i] << " ";
}
cout << "|" << endl;
cout << "|-----------------------------------------------|";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "| Number of digits matched :" << num_of_same_digits << " |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "|-----------------------------------------------|";
cout << "\n|\t \t \t \t \t \t|" << endl;
switch(num_of_same_digits)
{
case 1: cout << "| OH! You won Rs.100 |\n| Only one digit matched! |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "'-----------------------------------------------'\n";
break;
case 2: cout << "| OH! You won Rs.200 |\n| Only two digit matched! |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "'-----------------------------------------------'\n";
break;
case 3: cout << "| OH! You won Rs.300 |\n| Only three digit matched! |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "'-----------------------------------------------'\n";
break;
case 4: cout << "| OH! You won Rs.400 |\n| Only four digit matched! |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "'-----------------------------------------------'\n";
break;
case 5: cout << "| Congrats! You won grand prize of Rs.500 ONLY |\n| A l l d i g i t s m a t c h e d ! |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "'-----------------------------------------------'\n";
break;
default: cout << "| Sorry! You lose : TRY AGAIN LETER |";
cout << "\n|\t \t \t \t \t \t|";
cout << "\n'-----------------------------------------------'\n";
break;
}
}
/// Code by Asad Ali and officially posted on padacode.blogspot.com
Name: Asad Ali
Copyright: All Rights Reserved by JanGoO Group of Companies
Author: Tony Gaddies
Date: 15/01/19 00:12
Description: This program basically comparies user input
lottery number to the computer generated lottery ticket
number after comparing each digit, it show out put of
number of digits same in computer generated lottery
number and user entered lottery number.
Then there are some pre-fiexed prizes in the program:
If
1. One digit = prize of Rs.100
2. Two digits = prize of Rs.200
3. Three digits = prize of Rs.300
4. Four digits = prize of Rs. 400
5. Five digits = prize of Rs. 500
and program will output one of these prizes accourding to
the number of digits matched.
//// Computer Lottery Digits <compure with> User Lottery Digits
*/
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
const int SIZE = 5;
int lottery_arr[SIZE];
int user_arr[SIZE];
int num_of_same_digits = 0;
srand(time(NULL));
for ( int i = 0; i < 5; i++ )
{
lottery_arr[i] = rand() % 10;
}
cout << ".-----------------------------------------------.\n";
cout << "|\t \t \t \t \t \t|" << endl;
cout << "| Ticket has 5 digits for example : ";
for ( int i = 0; i < 5; i++ )
{
cout << lottery_arr[i] << " ";
}
cout << " |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "|-----------------------------------------------|";
cout << endl;
cout << "| Enter your lottery ticket digits one by one |" << endl;
cout << "'-----------------------------------------------'\nINPUT\\>\n";
for ( int i = 0; i < 5; i++ )
{
cin >> user_arr[i];
}
cout << endl;
cout << ".-----------------------------------------------.\n";
for ( int i = 0; i < 5; i++ )
{
if ( lottery_arr[i] == user_arr[i] )
{
num_of_same_digits = num_of_same_digits + 1;
}
}
cout << "| Winner Lottery ticket digits are : ";
for ( int i = 0; i < 5; i++ )
{
cout << lottery_arr[i] << " ";
}
cout << "|" << endl;
cout << "| User Lottery ticket digits a r e : ";
for ( int i = 0; i < 5; i++ )
{
cout << user_arr[i] << " ";
}
cout << "|" << endl;
cout << "|-----------------------------------------------|";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "| Number of digits matched :" << num_of_same_digits << " |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "|-----------------------------------------------|";
cout << "\n|\t \t \t \t \t \t|" << endl;
switch(num_of_same_digits)
{
case 1: cout << "| OH! You won Rs.100 |\n| Only one digit matched! |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "'-----------------------------------------------'\n";
break;
case 2: cout << "| OH! You won Rs.200 |\n| Only two digit matched! |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "'-----------------------------------------------'\n";
break;
case 3: cout << "| OH! You won Rs.300 |\n| Only three digit matched! |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "'-----------------------------------------------'\n";
break;
case 4: cout << "| OH! You won Rs.400 |\n| Only four digit matched! |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "'-----------------------------------------------'\n";
break;
case 5: cout << "| Congrats! You won grand prize of Rs.500 ONLY |\n| A l l d i g i t s m a t c h e d ! |";
cout << "\n|\t \t \t \t \t \t|" << endl;
cout << "'-----------------------------------------------'\n";
break;
default: cout << "| Sorry! You lose : TRY AGAIN LETER |";
cout << "\n|\t \t \t \t \t \t|";
cout << "\n'-----------------------------------------------'\n";
break;
}
}
/// Code by Asad Ali and officially posted on padacode.blogspot.com
Please help me improve my work by letting me know about mistakes (if any) I mad in the project given above, by commenting on the particular post.