即使某些数字匹配,程序也始终显示 0 个匹配项

时间:2021-02-09 01:54:17

标签: c++ function

<块引用>

程序应该有一个名为 lottery 的五个整数数组,并且应该为数组中的每个元素生成一个 0 到 9 范围内的随机数。

用户应输入五位数字,这些数字应存储在名为 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.\n\n";

    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 winner\n";
    else if (matches==3)
        cout << "";

} // end winnerOrLoser

1 个答案:

答案 0 :(得分:-1)

您的程序总是显示 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.\n\n";
    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 winner\n";
    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;
}