Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How would I create a vector of x amount of digits based on what the user inputed

ID: 3887298 • Letter: H

Question

How would I create a vector of x amount of digits based on what the user inputed. Then fill in the x amount of elements with a random number 0-9, using a pushback command. Then erase that possible digit from the first vector so it cannot be seen again. Should take about 3 lines. I have the code written below (C++).

// BullsCows.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>   

#include <algorithm>

#include <vector>

#include <string>

#include <random>

using namespace std;

vector<int>CnB;

vector<int>code;

int main() {

int bulls = 0;

int cows = 0;

int difficulty = 0;

int i = 0;

do {

cout << "Welcome to Cows and Bulls. Select your difficulty (3, 4, or 5)" << endl; //asking if the user wants to play with 3, 4, or 5 digits

cin >> difficulty;

} while (!(difficulty == 0 || difficulty == 3 || difficulty == 4 || difficulty == 5));// this loop keeps repeating until a proper number is chosen

random_device r_dev{};

default_random_engine d_engine{};

vector<int>CnB{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

vector<int>code;

for (i = 0; i < difficulty; ++i) {

uniform_int_distribution<> my_uni_dist{ 0, CnB() - 1 }; // pulling one random number from the CnB vector pool. These lines are the ones I am having trouble with.

int idx = code.push_back(d_engine); // I need to push.back the random number into this vector

CnB.erase(CnB.begin() + 2, CnB.begin() + 5); // need to erase that number so it cant be chosen again

}

return 0;

}

Explanation / Answer

Here is the code for above scenario:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote