程序应该有一个名为lottery的5个整数数组,并且应该为数组中的每个元素生成一个0到9范围内的随机数。
用户输入5位数字,存储在名为user的整数数组中。
程序将比较两个数组中对应的元素,并记录匹配的数字计数。例如,下面显示了彩票数组和用户数组,每个数组中都存储了样例数字。程序应该显示存储在彩票数组中的随机数和匹配位数。
如果所有数字都匹配,则显示一条消息,宣布用户成为大奖获得者。如果玩家只得到三次匹配,让他们根据随机生成的数字猜测1到20之间的数字。如果他们在两次尝试中猜对了数字,他们将获得500美元的现金奖励。否则,应该鼓励他们下次再买一张票。
代码:
#include <iostream> // for cin and cout streams
#include <cstdlib> // for the rand and srand functions
#include <ctime> // for the time function
// Constant Declarations
// ---------------------
const int lotteryDigits = 9;
const int SIZE = 5;
// Function Prototypes
// -------------------
void generateLottery(int[], int, int);
void userInput(int[], int);
int matchCounter(int[], int[], int);
void displayNumbers(int[], int[]);
void winnerOrLoser(int);
// -------------
// Main Function
// -------------
using namespace std;
int main()
{
// Variable Declarations
int lottery[5] = {0, 0, 0, 0, 0};
int user[5] = {0, 0, 0, 0, 0};
int matches = 0;
//Function Calls
generateLottery(lottery, SIZE, lotteryDigits);
userInput(user, SIZE);
matches = matchCounter(lottery, user, matches);
displayNumbers(lottery, user);
winnerOrLoser(matches);
system("pause");
return 0;
} //end main
// --------------------
// Function Definitions
// --------------------
// Randomly generates winning lottery numbers
void generateLottery(int lottery[], int, int)
{
unsigned seed = time(0);
srand(seed);
for (int count=0; count<SIZE; count++)
{
lottery[count] = 1 + rand() % lotteryDigits;
}
} // end generateLottery
// Reads user lottery number choices
void userInput(int user[], int)
{
cout << "This program will simulate a lottery.nn";
for (int count1=0; count1<SIZE; count1++)
{
cout << "Enter a digit between 0 and 9: ";
cin >> user[count1];
while (user[count1]<0 || user[count1]>9)
{
cout << "Error! Entry must be between 0 and 9: ";
cin >> user[count1];
}
}
} // end userInput
// Counts the number of matches
int matchCounter(int lotto[], int input[], int match)
{
bool numMatch = true;
for (int check = 0; check <= SIZE; check++)
{
if (lotto[check] != input[check])
numMatch = false;
check++;
}
return match;
} // end matchCounter
// Diplays the winning numbers and the user's numbers
void displayNumbers(int lottery[], int user[])
{
cout << "nThe winning lottery numbers are: " << lottery[0] << " " << lottery[1] << " " << lottery[2] << " " << lottery[3] << " " << lottery[4] << endl;
cout << "Your lottery numbers are: " << user[0] << " " << user[1] << " " << user[2] << " " << user[3] << " " << user[4] << endl;
} // end displayNumbers
//Displays the number of matches and whether or not the user has won
void winnerOrLoser(int matches)
{
cout << "You matched " << matches << " numbers";
if (matches == SIZE)
cout << "grand prize winnern";
else if (matches==3)
cout << "";
} // end winnerOrLoser
您的程序总是显示0位匹配,因为matchCounter
函数工作错误。你可以这样写。
int matchCounter(int lotto[], int input[], int match)
{
for (int i = 0; i < SIZE; ++i)
{
if (lotto[i] == input[i])
++match;
}
return match;
}
您还可以通过引用传递match
以使您的程序更快。这是完整的你的代码,我已经固定依靠你的问题:
#include <cstdlib> // for the rand and srand functions
#include <ctime> // for the time function
#include <iostream> // for cin and cout streams
using namespace std;
// Constant Declarations
// ---------------------
const int lotteryDigits = 9;
const int SIZE = 5;
// Randomly generates winning lottery numbers
void generateLottery(int lottery[]) {
srand(time(0));
for (int count = 0; count < SIZE; count++)
lottery[count] = rand() % (lotteryDigits + 1); // to get digits from 0 -> 9
}
// Reads user lottery number choices
void userInput(int user[]) {
cout << "This program will simulate a lottery.nn";
for (int count = 0; count < SIZE; ++count) {
cout << "Enter a digit between 0 and 9: ";
cin >> user[count];
while (user[count] < 0 || user[count] > 9) {
cout << "Error! Entry must be between 0 and 9: ";
cin >> user[count];
}
}
}
// Counts the number of matches
int matchCounter(int lotto[], int input[]) {
int match = 0;
for (int check = 0; check < SIZE; check++)
if (lotto[check] == input[check])
++match;
return match;
}
// Diplays the winning numbers and the user's numbers
void displayNumbers(int lottery[], int user[]) {
cout << "nThe winning lottery numbers are: ";
for (int i = 0; i < SIZE; ++i)
cout << lottery[i] << " ";
cout << "nYour lottery numbers are: ";
for (int i = 0; i < SIZE; ++i)
cout << user[i] << " ";
}
// Displays the number of matches and whether or not the user has won
void winnerOrLoser(int matches) {
cout << "You matched " << matches << " numbers";
if (matches == SIZE)
cout << "grand prize winnern";
else if (matches == 3)
cout << "";
}
// -------------
// Main Function
// -------------
int main() {
// Variable Declarations
int lottery[5];
int user[5];
int matches = 0;
//Function Calls
generateLottery(lottery);
userInput(user);
matches = matchCounter(lottery, user);
displayNumbers(lottery, user);
winnerOrLoser(matches);
system("pause");
return 0;
}