2016-11-29 136 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; 
} 
相關問題