c ++根据随机生成的数组检查用户输入的值

时间:2017-10-11 06:33:35

标签: c++ arrays

我在完成代码时遇到了一些问题。我的任务是创建一个程序,创建一个随机生成的大小为10的数组,任何数字在1-50之间。所有这些数字必须是唯一的。要求用户输入一个数字,然后程序应解密用户输入与数组中任意随机生成的数字匹配。我不能使用任何来自图书馆的花哨功能,我必须自己构建。到目前为止,这是我的代码。我在使用main内部的检查功能时遇到了问题。我不确定这是否仅仅是因为我缺乏知识(可能)或者当参数是数组时我不能这样做。任何帮助表示赞赏。

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>

using std::cin; using std::cout; using std::endl;

int check(int fillFunc[]){
    while(true){
        int val = 1 + rand() % 50; //assume it's unique
        bool unique = true;
        for (int i = 0; i < 10; ++i){  //if another number matches, it isn't unique, choose again
            if (fillFunc[i] == val){
                unique = false;
                break;
            }
        }
        //if it is unique, return it.
        if (unique){
            return val;
        }
    }

}

void draw(int fillFunc[]){
    for (int i = 0; i < 10; i++){
        fillFunc[i] = check(fillFunc);
    }
}

void printOut(int fillFunc[]){
    for (int i = 0; i < 10; i++){
        cout << " " << fillFunc[i];
    }
    cout << "\n";
}

int main(){


    srand((unsigned)time(NULL));

    const int arraySize = 10;
    int win[arraySize] = {};

    cout << "Please enter a number: ";
    int guess;
    cin >> guess;
    cout << "\n";

    draw(win);
            cout << "Congrats! Your number matches one from the lottery!";


    cout << "Your lottery numbers are: ";
    printOut(win);
    cout << "\n";

}

2 个答案:

答案 0 :(得分:1)

重新。你的answer

不确定我是否教过你&#34;正确的事情&#34;接着。我个人更喜欢这样写: Live On Coliru

#include <algorithm>
#include <set>
#include <random>
#include <cstdlib>
#include <ctime>
#include <iostream>

using Rng = std::mt19937;

template <unsigned N = 10, unsigned min = 1, unsigned max = 50>
class LottoDraw {
    using Number = unsigned;
    using Numbers = std::set<Number>;

    Numbers _numbers;
  public:
    explicit LottoDraw(Rng& rng) {
        std::uniform_int_distribution<Number> dist(min, max);

        while(_numbers.size()<N) {
            _numbers.insert(dist(rng));
        }
    }

    Numbers allWinning() const {
        return _numbers; 
    }

    bool isWinning(Number check) const {
        return _numbers.find(check) != _numbers.end();
    };
};

struct Lottery {
    using Draw = LottoDraw<10, 1, 50>;

    Draw makeDraw() {
        return Draw(rng);
    }

  private:
    Rng rng { std::random_device{} () };
};

int main() {
    Lottery lotto;
    auto draw = lotto.makeDraw();

    std::cout << "\nEnter a number: ";
    int guess;
    while (std::cin >> guess) {
        if (draw.isWinning(guess)) {
            std::cout << "\nCongrats! Your number matches one from the lottery!";
            break;
        }
        std::cout << "\nEnter a number: ";
    }

    std::cout << "\nThe winning numbers are:";
    for (auto n : draw.allWinning())
        std::cout << " " << n;

    std::cout << "\n";
}

图书馆资料: Live On Coliru

#include <cstdlib>
#include <ctime>
#include <iostream>

class LottoDraw {
    using Number = unsigned;
    using Numbers = Number[10];

    Numbers _numbers;
  public:
    LottoDraw() {
        for (auto& n : _numbers)
            n = rand()%51 + 1;
    }

    Numbers const& allWinning() const {
        return _numbers; 
    }

    bool isWinning(Number check) const {
        for (auto n : _numbers)
            if (check == n)
                return true;
        return false;
    };
};

struct Lottery {
    using Draw = LottoDraw;

    Lottery() {
        srand(time(0));
    }

    Draw makeDraw() {
        return Draw{};
    }
};

int main() {
    Lottery lotto;
    Lottery::Draw draw = lotto.makeDraw();

    std::cout << "\nEnter a number: ";
    int guess;
    while (std::cin >> guess) {
        if (draw.isWinning(guess)) {
            std::cout << "\nCongrats! Your number matches one from the lottery!";
            break;
        }
        std::cout << "\nEnter a number: ";
    }

    std::cout << "\nThe winning numbers are:";
    for (auto n : draw.allWinning())
        std::cout << " " << n;

    std::cout << "\n";
}

缺少的主要内容是

  1. 均匀随机分布
  2. 抽奖中的唯一数字
  3. 问题是什么

    你的问题是关于How do I use arrays in C++?:数组是来自C的小遗产而且表现不好&#34;以及#34;作为一流的类型。

    您可以通过使用数组的c ++ 11便捷拼写来回避所有这些:std::array删除所有这些:

    #include <array>
    
    using Number = unsigned;
    using Draw = std::array<Number, 10>;
    
    int check(Draw &draw);
    void draw(Draw &draw);
    void printOut(Draw const &draw);
    
    int main() {
        srand(time(0));
    
        Draw win{};
    

答案 1 :(得分:0)

我没有尝试调用check函数,而是使用已经在main中的win数组。

const int arraySize = 10;
    int win[arraySize] = {};

    cout << "Please enter a number: ";
    int guess;
    cin >> guess;


    draw(win);

    for (int i = 0; i < 10; ++i){
        if (win[i] == guess){
            cout << "\n";
            cout << "Congrats! Your number is a match! ";
        }
    }