-1

I need a way to generate a line string of 4 letters from the following 6 letters R,V,B,J,M,C, the same letter can be string multiple time since it's completely random. I've been looking for an answer all day and tried multiple suggested methods but nothing seem to work!

Help would be Greatly appreciated !

i'm not asking anybody to do my coding for me, only reaching out for help, i don't see any point in giving out my code if it has nothing to do with the question i'm asking, smh on you people and Thank You for the others taking some of their precious time to help!

here's my code so far, will keep it updated

#include "stdafx.h"
#include <vector> // pour les vectors
#include <string> // pour les string
#include <time.h>   // pour la fonction time();
#include <iostream> // pour le cout et le cin
#include <iomanip>  // pour les manipulateurs du cout: fixed,       setprecision(), setw(), ...
#include <conio.h>  // pour le _getch() et _getche()
#include <time.h>   // pour la fonction time()
#include "C:\Users\User1\Desktop\prog\cvm.h"// pour clrscr(), wherex(),   wherey(), gotoxy()

using namespace std; // Pour ne pas être obligé d'écrire std::cout

void main()
{
//VARIABLE
char R, V, B, J, M, C;
char debug;
int nbessai = 10; // nb of guesses
static const char choix[] = 'RVBJMC';
int stringLength = sizeof(choix) - 2;
string couleur[6] = { 'R','V','B','J','M','C'};
string essai;
string reponse = ;

int x, c; // pour gérer la ligne et la colonne du curseur

srand((int)time(NULL)); // initialisation du générateur de nombre aléatoire avec l'heure en seconde

//INPUT
cout << endl << setw(50) << "JEU DES COULEURS" << endl << endl << endl;
cout << setw(53) << "R\220GLAGES DE LA PARTIE" << endl << endl << endl;
cout << setw(45) << "Activer le mode en d\202bogage ? (O/N) : ";
c = wherex(); x = wherey();
do
{
    gotoxy(c, x);
    debug = toupper(_getch());
    cout << debug;
} while ((debug != 'O') && (debug != 'N') && (debug != 'o') && (debug != 'n')); // loop for debug mode

//OUPUT
clrscr(); // clean screen

cout << endl << setw(50) << "JEU DES COULEURS" << endl << endl << endl;
cout << setw(64) << "(R)ouge , (V)ert , (B)leu , (J)aune , (M)auve , (C)yan" << endl << endl;
cout << setw(10) << "#" << setw(11) << "Essais" << setw(22) << "Bien plac\202e(s)" << setw(20) << "Mal plac\202e(s)" << endl << endl;

for (int i = 0; i =< nbessai; i++)
{
    cout << setw(10) << i+1 << ") ";
    

}
_getch();



} 

it's mostly in french btw

Community
  • 1
  • 1
  • Welcome to Stack Overflow! Please provide the code that you've tried so far regarding your question. We'll be glad to help you but this site is not your personal coding service, so please show some coding effort. – haindl Nov 29 '16 at 20:41

3 Answers3

0

The easiest way I would imagine is to put the 6 letters into an array. Roll a random number 4 times to determine which letter to select, and append it to your string.

Thats the basic idea, I won't code it since you haven't shown too much effort, but the idea should suffice.

danielku97
  • 60
  • 8
0

Some very basic code to get you started:

#include <iostream>
#include <string>
#include <vector>
#include <ctime>

int main()
{
    std::vector<char> chars = { 'R','V','B','J','M','C' };
    std::string output;

    srand(static_cast<size_t>(time(0)));

    for (size_t i = 0; i < 4; ++i)
        output += chars[std::rand() % chars.size()];

    std::cout << output;

    return 0;
}

This can be improved in number of different ways, you have a nice explanation if you want to have better randomness here: get random element from container

Community
  • 1
  • 1
sharyex
  • 460
  • 1
  • 5
  • 17
0

Try this:

#include <iostream>
#include <string>
#include <time.h>

int main()
{
    srand(time(NULL));
    char myArray[6] = { 'R', 'V', 'B', 'J', 'M', 'C' };
    std::string myString;

    for (int i = 0; i < 4; i++)
    {
        int randomNum = rand() % 6;
        myString += myArray[randomNum];
    }

    std::cout << myString << std::endl;

    return 0;
}
yahiheb
  • 147
  • 9