rand into variable

Hi,

I'm new here and just started learning C++. I'm trying to recreate "Hang Man" game using only #include <iostream> (and apparently #include <ctime> and #include <cstdlib>). I've created all the necessary code to launch it, but I don't know how to make the animal selection random. Can anyone tell me how? I'm sorry for not using English, but I will translate as best as I can when asked. Thanks.
#include <iostream>
using namespace std;


int main()
{
//Keywords
char nama[100];
int tipe;
char yn,ulang;
char b1='_';
char b2='_';
char b3='_';
char b4='_';
char b5='_';
char b6='_';
char b7='_';
char b8='_';
char a1;
char a2[]="ULAR";
char a3[]="GARUDA";
char a4[]="KOMODO";
float binatang;
int kesempatan;



//Restart
do
{
//Penyebutan Nama
cout << "\n\n\nSelamat datang di permainan Hang Man! \n\n\nMasukkan nama Anda: ";
cin >> nama;
cout << "\nSelamat datang " << nama << "! \n\n\n";

//Penentuan Mode
do
{
//Pemilihan Mode
do
{
cout << "Mode apa yang ingin anda mainkan? \n 1. Zen \n 2. Classic \n";
cin >> tipe;
} while(tipe<=0||tipe>=3);

//Peraturan dan Konfirmasi
do
{
switch (tipe)
{
case 1:{
cout << "Aturan permainannya adalah seperti ini: Anda memiliki kesempatan sebanyak 10 kali. Tidak ada bantuan. \n";
cout << "Apakah Anda ingin bermain Zen? Y/N: ";
cin >> yn;
cout << "\n\n";
} break;

case 2:{
cout << "Aturan permainannya adalah seperti ini: Anda memiliki kesempatan sebanyak 5 kali, tetapi Anda akan diberikan Hint pada awal dan setiap kesalahan yang Anda buat \n";
cout << "Apakah Anda ingin bermain Classic? Y/N: ";
cin >> yn;
cout << "\n\n";
} break;
}
} while((yn!='Y')&&(yn!='N'));
cout << "\n";
} while(yn!='Y');

//Mulai Permainan

//Reset "kesempatan" dan slot huruf
kesempatan=0;
b1='_';
b2='_';
b3='_';
b4='_';
b5='_';
b6='_';
b7='_';
b8='_';

switch (tipe)
//Zen
{
case 1:
{
do
{
//Pemasukkan Huruf

cout << "\nKesempatan yang terpakai: " << kesempatan;
cout << "\n\nMasukkan huruf: ";
cin >> a1;
switch(a1)
{
case 'G': b1=a3[0]; break;
case 'A': b2=a3[1], b6=a3[5]; break;
case 'R': b3=a3[2]; break;
case 'U': b4=a3[3]; break;
case 'D': b5=a3[4]; break;
default: kesempatan++;
}

//Output GARUDA
cout << "\n";
cout << b1;
cout << b2;
cout << b3;
cout << b4;
cout << b5;
cout << b6;
cout << "\n\n";
} while(((b1!='G')||(b2!='A')||(b3!='R')||(b4!='U')||(b5!='D')||(b6!='A'))&&(kesempatan<10));
} break;
//Classic
case 2:
{
do
{
//Pemasukkan Huruf
switch (kesempatan)
{
case 0: cout << "\nBinatang ini merayap"; break;
case 1: cout << "\nBinatang ini ada yang berbahaya dan ada yang tidak"; break;
case 2: cout << "\nBeberapa binatang ini dapat mengeluarkan bisa"; break;
case 3: cout << "\nBinatang ini berdarah dingin"; break;
case 4: cout << "\nBinatang ini kecil dan panjang"; break;
}
cout << "\nKesempatan yang terpakai: " << kesempatan;
cout << "\n\nMasukkan huruf: ";
cin >> a1;
switch(a1)
{
case 'U': b1=a2[0]; break;
case 'L': b2=a2[1]; break;
case 'A': b3=a2[2]; break;
case 'R': b4=a2[3]; break;
default: kesempatan++;
}

//Output ULAR
cout << "\n";
cout << b1;
cout << b2;
cout << b3;
cout << b4;
cout << "\n\n";
} while(((b1!='U')||(b2!='L')||(b3!='A')||(b4!='R'))&&(kesempatan<5));
} break;
};


//Menang atau Kalah
if(kesempatan<10)
cout << "\nHore! Anda Menang!\n";
else if(kesempatan=10)
cout << "\nMaaf, Anda kalah!\n";


//Pemilihan Restart
do
{
cout << "\nApakah anda ingin bermain lagi? Y/N: ";
cin >> ulang;
}while((ulang!='Y')&&(ulang!='N'));
cout << "\n\n\n";
}while(ulang=='Y');

cout << "Terima kasih telah bermain " << nama << "!\n\n\n";

return 0;
"new here":
Code tags make posted code easier to read. See: http://www.cplusplus.com/articles/jEywvCM9/

"learning C++":
Then focus on C++ rather than C. The <ctime> and <cstdlib> are C. C++ has <random> (and <chrono>).
http://www.cplusplus.com/reference/random/
http://www.cplusplus.com/reference/random/mersenne_twister_engine/mersenne_twister_engine/
http://www.cplusplus.com/reference/random/uniform_int_distribution/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <chrono>
#include <random>

int main ()
{
  unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
  std::mt19937 g1 ( seed1 );

  std::uniform_int_distribution<int> distribution( 0, 2 );
  const char* beast[] { "cat", "hound", "parrot" };

  int dice_roll = distribution( g1 ); // take a random number
  std::cout << beast[dice_roll] << '\n'; // and use it
}
1
2
3
4
5
6
7
8
9
10
11
12
char b1='_';
char b2='_';
char b3='_';
char b4='_';
char b5='_';
char b6='_';
char b7='_';
char b8='_';
char a1;
char a2[]="ULAR";
char a3[]="GARUDA";
char a4[]="KOMODO";
Any time you find yourself writing variables like this you should ask yourself if an array is more appropriate:
1
2
char b[8]{'_'};
const char *a[] = {"ULAR", "GARUDA", "KOMODO"};
Topic archived. No new replies allowed.