#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#include <vector>
using namespace std;
int main()
{
srand(time(0));
int rejouer=1;
int randMax=5;
while (rejouer==1)
{
vector<int> tableau;
int parties=0;
int numeroHasard=0;
for (int j=0; j<randMax; j++)
{
if (parties==0)
{
numeroHasard = ((rand() % randMax) +1);
tableau.push_back(numeroHasard);
parties+=1;
}
else
{
numeroHasard = ((rand() % randMax) +1);
for (int i=0; i<randMax; i++)
{
while (numeroHasard==tableau[i])
{
numeroHasard = ((rand() % randMax) +1);
}
}
tableau.push_back(numeroHasard);
}
}
for (int i=0; i<randMax; i++)
{
cout << "tableau[" << i << "] : " << tableau[i] << "." << endl;
}
getch();
system("cls");
}
return 0;
}
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#include <vector>
using namespace std;
int main()
{
srand(time(0));
int rejouer=1;
int randMax=5;
while (rejouer==1)
{
vector<int> tableau;
int parties=0;
int numeroHasard=0;
int collision=1;
for (int j=0; j<randMax; j++)
{
if (parties==0)
{
numeroHasard = ((rand() % randMax) +1);
tableau.push_back(numeroHasard);
parties+=1;
}
else
{
numeroHasard = ((rand() % randMax) +1);
while (collision>=1)
{
for (int i=0; i<randMax; i++)
{
if (numeroHasard==tableau[i])
{
collision+=1;
for (int i=0; i<randMax; i++)
{
numeroHasard = ((rand() % randMax) +1);
}
}
else
collision=0;
}
}
tableau.push_back(numeroHasard);
}
}
for (int i=0; i<randMax; i++)
{
cout << "tableau[" << i << "] : " << tableau[i] << "." << endl;
}
getch();
system("cls");
}
return 0;
}
#include <iostream> #include <vector> #include <algorithm> #include <fstream> #include <cstdlib> // Charge les mots à partir d'un fichier ici // Retourne si le dictionnaire a été correctement chargé (fichier trouvé, fichier correctement formé) bool load(const std::string& filename, std::vector<std::string>& words) { std::ifstream file(filename.c_str()); if (!file) return false; // À toi de jouer. return true; } // Joue une partie, sur un mot. void play(const std::string& word) { // Ici fait deviner le mot "word" } int main() { srand(time(0)); const std::string filename = "dico.txt"; std::vector<std::string> words; if (!load(filename, words)) { std::cerr << "Can't load dictionnary: " << filename << std::endl; return 1; } bool rejouer = true; int max = words.size(); int parties = max; // On se met en position: tous les mots ont été devinés. while (rejouer) { // Si on a fait deviner tous les mots. if (parties == max) { parties = 0; // Mélange les mots au sein du tableau. std::random_shuffle(words.begin(), words.end()); } play(words[parties]); ++parties; std::cout << "Rejouer ? [1/0]: "; std::cin >> rejouer; } //system("pause"); // Pour Windows return 0; }
Améliorez votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature