2016-11-29 32 views
-1

我需要一种方法来从下列6个字母R,V,B,J,M,C生成4个字母的行字符串,因为它是完全随机的,因此可以多次使用字符我一直在寻找一个答案,并尝试了多个建议的方法,但似乎没有任何工作!如何从C++中选择6个字母生成4个字母的行

帮助将不胜感激!

我不是要求任何人为我做我的编码,只是伸出援手,我没有看到我的代码没有任何意义,如果它与我问的问题无关,对你们的人民表示感谢,并感谢其他人花费宝贵的时间帮助他人!

这里是我到目前为止的代码,会随时更新

#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(); 



} 

它主要是在法国BTW

+0

欢迎来到Stack Overflow!请提供您迄今为止尝试过的关于您的问题的代码。我们很乐意为您提供帮助,但本网站不是您的个人编码服务,因此请出示一些编码工作。 – haindl

回答

0

我可以想象的最简单方法是将6个字母为阵列。 滚动一个随机数字4次以确定选择哪个字母,并将其附加到您的字符串中。

这就是基本的想法,我不会编码它,因为你没有显示太多的努力,但这个想法应该足够了。

1

一些非常基本的代码,让你开始:

#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; 
} 

这可以用不同的方式数来提高,你有一个很好的解释,如果你想有更好的随机性这里:get random element from container

0

尝试这个:

#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; 
}