Randomizer of words in an array

You need to write a randomizer of words that takes 6 letters from the keyboard,writes them in the " alphabet "and then makes random words,of random length,using letters from the"alphabet".The number of words is from 9 to 12.So far, I have achieved that I get 1 random word and write it to the array Slovaa The code itself. P. s.There is a compiler error, type multiple access to the array.Also emphasizes my strange condition and the line Slovo[7] = { 0 }, saying that the array has 7 bytes,but it may need 11, and in the line I gave above - the array has 7 bytes,but may need 8.

P.S.S.Advise me in which direction to go, most likely I have already climbed far into the wilds here,but I wanted to get something similar, only working:)Thank you in advance

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <string>
#include "alphabet.h"
using namespace std;

void Alphabet(char* arr)  //алфавит
{
    cout << "Введите 6 букв вашего алфавита: " << endl;
    for (int i = 0; i < 6; i++)
    {
        cin >> arr[i]; //Ввод букв для слов
    }
}

int main(int argc, char* argv[])
{
    setlocale(0, "RU");
    srand(time(NULL));
    char arr[6];
    Alphabet(arr);

    TextRewrite objTextRewrite;  //заготовка под класс

    string sentence[20];   //массив предложений,состоящий из слов
    string Podlejashie[4]; //массив слов,которые будут ставиться в предложении на место подлежащего
    string Skazuemoe[4]; //массив слов,которые будут ставиться в предложении на место сказуемого
    string Glagol[4]; //массив слов,которые будут ставиться в предложении на место глагола
    char Slovo[7];  //само слово,которое будет улетать в рандомный массив 1-3 слово,изначально хотел обнулять этот массив,чтобы 
                    //использовать только 1 массив  
    int countOfWords = rand() % 3 + 9;  // колличество слов в предложении
    int count = -1; //счетчик слов,планировал с помощью него выйти из цикла,когда заполнится массив слов

    for (int i = 0; i < countOfWords; i++)  // цикл задачи слов
    {
        count++;  

        Slovo[i] = arr[rand() % 6 + 1]; //рандомная генерация слов из алфавита,но не понял,как играться с длиной слова,чтобы и она была рандомной
        if (Slovo[countOfWords - 1] == arr[0] || Slovo[countOfWords - 1] == arr[1] || Slovo[countOfWords - 1] == arr[2] || Slovo[countOfWords - 1] == arr[3] || Slovo[countOfWords - 1] == arr[4] || Slovo[countOfWords - 1] == arr[5]) // просто ужасное условие,сам знаю,буду рад,если подскажите как исправить,проверяет на "готовность" слова,то бишь,до конца оно сгенерировалось или нет
        {
            Podlejashie[i] = Slovo; //собсна,само присваение массиву слов сгенерированного слова
            Slovo[7] = { 0 }; //попытка обнулить массив,хотя сам не понимаю зачем уже это делал
        }
        if (count == countOfWords - 1) // тут выход из цикла,если массив слов заполняется
        {
            break;
        }
    }

    //for (int i = 0; i < 6; i++)  //тест вывода
    //  cout << Slovaa[i];
    return 0;
}
Author: Harry, 2020-02-19

1 answers

As it seems to me, about what you want:

string alphabet;
cin >> alphabet;

int wordsCount = 9 + rand()%4;  // Случайное количество слов
for(int i = 0; i < wordsCount; ++i)
{
    int wordLength = 5 + rand()%10;  // Случайная длина слова
    string word;
    for(int j = 0; j < wordLength; ++j)
        word += alphabet[rand()%alphabet.length()];
    cout << word << endl;
}

See how it works-and you can redo your code. Of course, in a good way, you need to use <random> in 2020, but this is the second question :) Well, it is clear that instead of displaying words, you do what you need with them...

An example of the work is here.

 1
Author: Harry, 2020-02-19 12:36:29