1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
//This program consist of ,,,,, functions. It prompt the user to
#include <iostream>
#include <cstdlib>
using namespace std;
//prototype
void showIntro();
int getWinNumb();
int searchNumber(int [], int, int);
//Global variable.
const int SIZE = 10;
const int DIGIT = 6;
int main()
{
//To hold the 5-digit lucky combination of the user.
int luckyNumb[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121};
int winNumb; //To hold the combination transformed into integer variable.
int results; //To hold the result of the search function
char repeat; //To ask user to repeat the program or not.
do
{
//Displays introduction message.
showIntro();
//Get user 5-digits choice.
winNumb = getWinNumb();
//Search the winning number.
results = searchNumber(luckyNumb, SIZE, winNumb);
// If searchList returned -1, then 100 was not found.
if (results == -1)
cout << "You did not make the winning combination\n";
else
{
// Otherwise results contains the subscript of
// the first winNumb in the array.
cout << "This is a winning combination number ";
cout << (results + 1) << endl;
}
//Ask the user to repeat de program if necessary
cout <<"Do you want to rerun the pregram?\n";
cout <<"Enter Y or y for Yes or N or n for No\n";
cin >> repeat;
}while((repeat == 'y')||(repeat == 'y'));
system("pause");
return 0;
}
//*****************************************************************
// The getWinNumb function performs the string transformation *
// into an integer variable. It accept a string variable as *
// an argument and returns an interger. *
//*****************************************************************
int getWinNumb()
{
int numNumber; //To hold a numeric data converted from a string.
char strNumber[DIGIT]; //To hold the user's string input.
int length; //To hold the length of the input.
//Get the user combination number.
cout << "\nEnter your 5-digit Combination number" <<endl;
cin.getline(strNumber, DIGIT);
length = strlen(strNumber);
//Length's input validation
while(length!= 5)
{
cout <<"Error:You most Enter a 5-digit number\n";
cout <<"Enter another Number\n";
cin.getline(strNumber, DIGIT);
length = strlen(strNumber);
}
numNumber = atoi(strNumber);
return numNumber;
}
//*****************************************************************
// The searchNumbers function performs a linear search on an *
// integer array. The array list, which has a maximum of numElems *
// elements, is searched for the number stored in value. If the *
// number is found, its array subscript is returned. Otherwise, *
// -1 is returned indicating the value was not in the array. *
//*****************************************************************
int searchNumber(int list[], int numElems, int combNumb)
{
int index = 0; // Used as a subscript to search array
int position = -1; // To record position of search value
bool found = false; // Flag to indicate if the value was found
while (index < numElems && !found)
{
if (list[index] == combNumb) // If the value is found
{
found = true; // Set the flag
position = index; // Record the value's subscript
}
index++; // Go to the next element
}
return position; // Return the position, or -1
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
// The function showIntro displays the introduction ^
//message of the program. ^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
void showIntro()
{
cout << "\t##############################################################\n";
cout << "\t# This program prompt a lottery player to enter his #\n";
cout << "\t# 5-digits combination number for the weekly lottery #\n";
cout << "\t# Then the program performs a search from the player's #\n";
cout << "\t# possible numbers and reports or not he mad the good choice.#\n";
cout << "\t##############################################################\n";
}
| |