簡體   English   中英

在C ++中將數組傳遞給函數? [彩票號碼]

[英]Passing Arrays into Functions in C++? [Lottery Numbers]

我正在用C ++創建一個彩票游戲。 我創建了一個名為GetLottoPicks的函數,它向用戶詢問7個數字。 我有另一個名為GenWinNums的函數,可生成7個隨機數。

問題是GenWinNums它一直產生這7次:-858993460

我相信我對WinningNums []中的參數輸入顯然不是嗎?

謝謝。

int main()
{
string name;
int choice;
int user_picked[7];
int chosen_lotto[7];

cout << "LITTLETON CITY LOTTO MODEL:" << endl;
cout << "---------------------------" << endl;
cout << " 1) Play Lotto " << endl;
cout << " Q) Quit Program " << endl;
cout << " Please make a selection: ";
cin >> choice;

if (choice == 1)
{
    cout << "\n Please enter your name: ";
    cin >> name;

    getLottoPicks(user_picked);
    GenWinNums(chosen_lotto);

    for(int i = 0; i < 7; i++)
    {
        cout << chosen_lotto[i];    
    }

    for(int i = 0; i < 7; i++)
    {
        cout << user_picked[i];
    }
}

else if (choice == 'Q' || 'q')
{
    exit(0);
}

else 
{
    cout << "That is not a valid choice." << endl;
}

 system("PAUSE");
 return 0;
 }

 int getLottoPicks(int UserTicket[])
 {   
const int amount = 7;
UserTicket[amount];
int ticket = UserTicket[amount];

for (int i = 0; i < amount; i++)
{
    cout << "\n Please enter number " << i + 1 << ":";
    cin >> UserTicket[i];

    while (UserTicket[i] < 1 || UserTicket[i] > 40)

    {
        cout << "Your selection cannot be less than 1 and/or greater than 40. Try Again." << endl;
        cin >> UserTicket[i];
    }

if(i > 0)
    {
        for(int check = 0; check < i; check++)
        {
            while(UserTicket[i] == UserTicket[check])
            {
                cout << "You cannot use the same lotto number twice. Try again: " << endl;
                cin >> UserTicket[i];
            }
        }
}

}

return ticket;
 }

 int GenWinNums(int WinningNums[])
 {
const int amount = 7;
int numbers[amount];
int ticket = WinningNums[amount];

for(int i = 0; i < amount; i++)
{
    numbers[i] = (rand() % 40) + 1;

    while(numbers[i] < 1 || numbers[i] > 40)
    {
        numbers[i] = (rand() % 10) + 1;
    }
    if(i > 0)
    {
        for(int check = 0; check < i; check++)
        {
            while(numbers[i] == numbers[check])
            {
                numbers[i] = (rand() % 10) + 1;
            }
        }
    }
}
return ticket;
 }

只是為了好玩,這里有相當多的修復。 我不打算列舉所有這些。 基本上:

  • 通過引用傳遞固定大小的數組,以避免不安全的使用
  • 嚴格檢查輸入錯誤,並在單個循環中執行
  • 幾個錯誤的數組賦值,它們不做任何事情或阻止調用者看到結果......
  • 使用char作為輸入類型(因此Q會匹配)並且更喜歡切換重復的ifs
  • 請參閱下面的更典型的C ++樣式解決方案,包括檢查WIN或LOSE

為讀者練習:為你的rand()生成播種 ,或者贏得這個彩票非常簡單(粘貼在之前的中獎票:))

演示運行時的輸出(也顯示錯誤處理):

LITTLETON CITY LOTTO MODEL:
---------------------------
 1) Play Lotto 
 Q) Quit Program 
Please make a selection: 1

 Please enter your name: me

 Please enter number 1:34

 Please enter number 2:7

 Please enter number 3:16

 Please enter number 4:18

 Please enter number 5:24

 Please enter number 6:24
Duplicate entry 24
 Please enter number 6:7
Duplicate entry 7
 Please enter number 6:37

 Please enter number 7:whoops
Bad input 'whoops' discarded
 Please enter number 7:what was the last number again?
Bad input 'what' discarded
 Please enter number 7:Bad input 'was' discarded
 Please enter number 7:Bad input 'the' discarded
 Please enter number 7:Bad input 'last' discarded
 Please enter number 7:Bad input 'number' discarded
 Please enter number 7:Bad input 'again?' discarded
 Please enter number 7:27
User ticket:    7; 16; 18; 24; 27; 34; 37; 
Winning ticket: 7; 16; 18; 24; 27; 34; 36; 
Result of draw: WINNER

#include <string>
#include <iostream>

const size_t amount = 7;

void getLottoPicks(int (&ticket)[amount])
{
    for(int i = 0; i < amount; i++)
    {
        do
        {
            std::cout << "\n Please enter number " << i + 1 << ":";
            if(std::cin >> ticket[i])
            {
                if(ticket[i] >= 1 && ticket[i] <= 40)
                {
                    bool ok = true;
                    for(int check = 0; ok && check < i; check++)
                    {
                        ok &= (ticket[i] != ticket[check]);
                    }
                    if(ok)
                    {
                        break;
                    }
                    else
                    {
                        std::cout << "You cannot use the same lotto number twice. Try again." << std::endl;
                    }
                }
                else
                {
                    std::cout << "Your selection cannot be less than 1 and/or greater than 40. Try Again." << std::endl;
                }
            }
            else
            {
                std::cin.clear();
                std::string discard;
                std::cin >> discard;
                std::cout << "Bad input '" << discard << "' discarded";
            }
        }
        while(true);
    }
}

void GenWinNums(int (&winningNumbers)[amount])
{
    for(int i = 0; i < amount; i++)
    {
        winningNumbers[i] = (rand() % 40) + 1;
        while(winningNumbers[i] < 1 || winningNumbers[i] > 40)
        {
            winningNumbers[i] = (rand() % 10) + 1;
        }
        if(i > 0)
        {
            for(int check = 0; check < i; check++)
            {
                while(winningNumbers[i] == winningNumbers[check])
                {
                    winningNumbers[i] = (rand() % 10) + 1;
                }
            }
        }
    }
}
int main()
{
    std::string name;
    char choice;
    std::cout << "LITTLETON CITY LOTTO MODEL:" << std::endl;
    std::cout << "---------------------------" << std::endl;
    std::cout << " 1) Play Lotto " << std::endl;
    std::cout << " Q) Quit Program " << std::endl;
    std::cout << " Please make a selection: ";
    std::cin >> choice;
    switch(choice)
    {
        case '1':
            {
                std::cout << "\n Please enter your name: ";
                std::cin >> name;

                int user_picked[amount];
                int chosen_lotto[amount];

                getLottoPicks(user_picked);
                GenWinNums(chosen_lotto);

                std::cout << "\n";
                for(int i = 0; i < amount; i++)
                {
                    std::cout << user_picked[i] << "; ";
                }
                std::cout << "\n";
                for(int i = 0; i < amount; i++)
                {
                    std::cout << chosen_lotto[i] << "; ";
                }
            }
        case 'Q':
        case 'q':
            return 0;
        default:
            std::cout << "\n That is not a valid choice." << std::endl;
    }
    std::cout << "\n Press enter...";
    std::cin >> choice;
}

更典型的C ++解決方案

#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <set>

const size_t amount = 7;

typedef std::set<int> ticket_t;

ticket_t getLottoPicks()
{
    ticket_t ticket;
    while (ticket.size() < amount)
    {
        std::cout << "\n 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 << "LITTLETON CITY LOTTO MODEL:" << std::endl;
    std::cout << "---------------------------" << std::endl;
    std::cout << " 1) Play Lotto "             << 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 << "\n Please enter your name: ";
                std::string name;
                std::cin  >> name;

                const ticket_t user    = getLottoPicks();
                const ticket_t winning = genWinNums();

                std::cout << "User ticket:    " << user    << "\n";
                std::cout << "Winning ticket: " << winning << "\n";

                // 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") << "\n";
            }
        case 'Q': case 'q':
            return 0;
        default:
            std::cout << "\n That is not a valid choice." << std::endl;
    }

    std::cin.ignore(1<<24,'\n');
    std::cout << "\n Press enter...";
    std::string dummy;
    std::getline(std::cin, dummy);
}

看起來你永遠不會在例程中為WinningNums數組賦值,這可能解釋了為什么你WinningNums得到相同的數字。

我建議刪除numbers數組並直接使用WinningNums生成隨機數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM