Как сделать так, чтобы переменная person принимала 0000

Рейтинг: -4Ответов: 1Опубликовано: 11.07.2023
#include<locale.h>
#include<iostream>
#include<random>
#include<string>
#include<vector>
#include<limits>

void random_pc(std::vector<int>& pc)
{
    std::random_device rd;
    std::mt19937 q(rd());

    for (int i = 0; i < pc.size(); i++)
    {
        std::uniform_int_distribution<int> dist(0, 9);
        pc[i] = dist(q);
    }
}
void division_number(std::vector<int>& arr_of_person, int& person)
{
    int division_person = 1000;
    for (int i = 0; i < 4; i++)
    {
        arr_of_person[i] = person / division_person;
        person -= (arr_of_person[i] * division_person);
        division_person /= 10;
    }
}
void count_bull_and_cow(std::vector<int>& arr_of_person, std::vector<int>& pc, int& bull, int& cow)
{
    for (int i = 0; i < 4; i++)
    {
        if (arr_of_person[i] == pc[i])
        {
            bull++;
        }
        else
        {
            for (int k = 0; k < 4; k++)
            {
                if (arr_of_person[i] == pc[k] && arr_of_person[k] != pc[k])
                {
                    cow++;
                    break;
                }
            }
        }
    }
}
void show_bull_and_cow(int& bull, int& cow)
{
    std::cout << bull << " быков; " << cow << " коров;" << std::endl;
}



int main()
{
    setlocale(LC_ALL, "rus");

    std::vector<int> pc(4);
    std::vector<int> arr_of_person(4);
    std::string exit;
    int person;

    while (true)
    {
        random_pc(pc);
        std::cout << "Введите четырёх значное число!" << std::endl;
        while (true)
        {
            int cow = 0;
            int bull = 0;
            while (!(std::cin >> person) || person < 1000 || person > 9999)
            {
                std::cout << "Ошибка!" << std::endl;
                std::cout << "Введите четырёх значное число!" << std::endl;

                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }


            division_number(arr_of_person, person);
            count_bull_and_cow(arr_of_person, pc, bull, cow);
            show_bull_and_cow(bull, cow);

            if (bull == 4)
            {
                break;
            }
        }
        std::cout << "Если вы хотите закончить игру, нажмите 'x' или любой другой символ чтобы продолжить!" << std::endl;
        std::cin >> exit;
        if (exit == "x")
        {
            std::cout << "Спасибо за участие!" << std::endl;
            break;
        }
        else
        {
            std::cout << "Продолжаем играть!" << std::endl;
        }
    }
    return 0;
}

Переменная person не должна принимать меньше четырёхзначного числа, но должна принимать 0000

Ответы

▲ 1

Переменная person имеет тип int, значит может быть только целым числом. Число 0000 это 0, а 0 меньше четырехзначного числа. Так что никак не сделать.