Home c++ Randomizer of words in an array

Randomizer of words in an array

Author

Date

Category

You need to write a word randomizer that takes 6 letters from the keyboard, writes them into the “alphabet” and then makes random words of random length using letters from the “alphabet”. The number of words is from 9 to 12. 1 random word and written to the Slovaaarray
The code itself.
Ps A compiler error comes out, type multiple access to the array. Also underlines my dumb condition and the line Slovo[7] = { 0 }, they say, the array has 7 bytes, but 11 may be required, and in the line, which I gave above – the array has 7 bytes, but 8 may be required.

P.S.S. Please advise in which direction to go, most likely I have already climbed far into the jungle here, but I wanted to get something similar, only working 🙂 Thanks in advance

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <string>
#include "alphabet.h"
using namespace std;
void Alphabet(char* arr)  //alphabet
{
    cout << "Enter6   alphabet: " << endl;
    for (int i = 0; i < 6; i++)
    {
        cin >> arr[i]; //Entering letters for words
    }
}
int main(int argc, char* argv[])
{
    setlocale(0, "RU");
    srand(time(NULL));
    char arr[6];
    Alphabet(arr);
    TextRewrite objTextRewrite;  //preparation for the class
    string sentence[20];   //an array of sentences consisting of words
    string Podlejashie[4]; //an array of words that will be put in the sentence in place of the subject
    string Skazuemoe[4]; //an array of words that will be put in the sentence in place of the predicate
    string Glagol[4]; //an array of words that will be put in the place of the verb in the sentence
    char Slovo[7];  //the word itself, which will fly away into a random array1-3 word, originally wanted to zero this array so that
                    //use only1 array
    int countOfWords = rand() % 3 + 9;  // number of words in a sentence
    int count = -1; //word counter, planned to use it to exit the loop when the word array is full
    for (int i = 0; i < countOfWords; i++)  // word task cycle
    {
        count++;  
        Slovo[i] = arr[rand() % 6 + 1]; //    alphabet,  ,    ,    
        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]) // just a terrible condition, I know myself, I will be glad if you tell me how to fix it, checks for"willingness" words, that is, to the end it was generated or not
        {
            Podlejashie[i] = Slovo; //sobsna, the very assignment of the generated word to an array of words
            Slovo[7] = { 0 }; //an attempt to zero out an array, although I myself do not understand why I already did it
        }
        if (count == countOfWords - 1) // here exit from the loop if the word array is filled
        {
            break;
        }
    }
    //for (int i = 0; i < 6; i++)  //output test
    //  cout << Slovaa[i];
    return 0;
}

Answer 1, authority 100%

I think this is roughly what you want:

string alphabet;
cin >> alphabet;
int wordsCount = 9 + rand()%4;  // Random word count
for(int i = 0; i < wordsCount; ++i)
{
    int wordLength = 5 + rand()%10;  // Random word length
    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, for good reason, you need to use in 2020 <random>, but this is the second question 🙂 Well, it is clear that instead of outputting words, do what you need with them …

An example of how it works – here .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions