Lottery Code - C++ - Matrix and Vectors

Hello people!

I am making a code in C ++ that simulates a lottery, the user enters 6 whole numbers, the code shows the numbers in a 6x10 matrix, performs the draw and shows how many numbers the selected user. I'm having trouble generating a matrix, can someone help me?


The matrix should look like this in a form similar to this [Bet Card]:

-->Chose the 1 number: 17
-->Chose the 2 number: 01
-->Chose the 3 number: 22

-----------------------------
TICKET CARD
------------------------------
01 -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- 17 -- -- --
-- 22 -- -- -- -- -- -- -- --



Here is the code I have so far (
Sorry the name of the variables are in Portuguese):

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

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

const size_t amount = 6;

typedef std::set<int> bilhete_t;

bilhete_t NumerosUsuario()
{
    bilhete_t bilhete;
    while (bilhete.size() < amount)
    {
        std::cout << "\n Por Favor entre com o numero " << (bilhete.size()+1) << ":";

        int entry;
        if(std::cin >> entry)
        {
            if(entry >= 1 && entry <= 60)
            {
                if (bilhete.end() != bilhete.find(entry))
                    std::cout << "Numero duplicado "  << entry;
                else
                    bilhete.insert(entry);
            } else
                std::cout << "Numero fora do intervalo [1..60]: "  << entry;

            continue;
        }

        std::cin.clear();
        std::string discard;
        std::cin >> discard;
        std::cout << "Bad input '" << discard << "' discarded";
    }
    return bilhete;
}

bilhete_t NumerosSorteados()
{
    bilhete_t bilhete;
    while (bilhete.size() < amount)
        bilhete.insert(rand() % 40 + 1);
    return bilhete;
}

std::ostream& operator<<(std::ostream& os, bilhete_t const& t)
{
    std::copy(t.begin(), t.end(), std::ostream_iterator<int>(os, "; "));
    return os;
}

int main()
{
    std::cout << "LOTERIA MEGA MALUCA:" << std::endl;
    std::cout << "---------------------------" << std::endl;
    std::cout << " 1) Jogar a Loteria "             << std::endl;
    std::cout << " 2) Sair do programa "           << std::endl;
    std::cout << "Por Favor faca uma selecao: ";
    char choice;
    std::cin >> choice;

    switch(choice)
    {
        case '1':
            {
                std::cout << "\n Por Favor, digite o seu nome: ";
                std::string name;
                std::cin  >> name;

                const bilhete_t user    = NumerosUsuario();
                const bilhete_t winning = NumerosSorteados();

                std::cout << "Bilhete do Usuario:    " << user    << "\n";
                std::cout << "Bilhete vencedor: " << winning << "\n";

                // Checa se venceu?
                bool ok = 0 == std::lexicographical_compare(
                        user.begin(), user.end(),
                        winning.begin(), winning.end());

                std::cout << "Resultado da aposta: " << (ok? "VENCEDOR":"Nao foi dessa vez") << "\n";
            }
        case '2':
            return 0;
        default:
            std::cout << "\n Essa nao e uma escolha valida." << std::endl;
    }

    std::cin.ignore(1<<24,'\n');
    std::cout << "\n Pressione enter...";
    std::string dummy;
    std::getline(std::cin, dummy);
}
Last edited on
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
#include <iostream>
#include <iomanip>
#include <set>
#include <algorithm>
#include <random>

const size_t NumValues = 6;
const int MaxValue = 60;

std::default_random_engine RndEngine{std::random_device{}()};

typedef std::set<int> bilhete_t;

std::ostream& operator<<(std::ostream& os, bilhete_t const& t)
{
    os << "\n-----------------------------\n"
       << "         TICKET CARD\n"
       << "-----------------------------";
    int i = 0;
    os << std::setfill('0');
    for (const auto& x: t) {
        while (true) {
            if (i % 10 == 0) os << '\n';
            if (++i == x) break;
            os << "-- ";
        }
        os << std::setw(2) << x << ' ';
    }
    while (++i <= MaxValue) {
        if ((i - 1) % 10 == 0) os << '\n';
        os << "-- ";
    }
    return os << std::setfill(' ');
}

bilhete_t NumerosSorteados()
{
    std::uniform_int_distribution<int> dist(1, MaxValue);
    bilhete_t bilhete;
    while (bilhete.size() < NumValues)
        bilhete.insert(dist(RndEngine));
    return bilhete;
}

int main()
{
    auto t = NumerosSorteados();
    std::cout << t << '\n';
}

Last edited on
Topic archived. No new replies allowed.