First of all, this code is more C code than C ++ code. But after solving your problem with randomize()
is able to compile for either of them. randomize()
is a Borland style function, although it may still be available in some compilers, it is not part of the standard. The way to give a new seed to the random number generator so that each time the program is run the results are different, it is by srand()
which including the header file time.h allows call function time(NULL)
, finally replacing non-standard randomize();
with srand(time(NULL));
Your code looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char* argv[])
{
char *v[3] = {"piedra", "Papel", "tijeras"};
int ju, pc, jugados = 1, ganados = 0, perdidos = 0;
srand(time(NULL));
do
{
printf("Jugada %d, elija una opcion (1, 2, 3) G: %d P: %d\n", jugados, ganados, perdidos);
printf ("1. Piedra\n");
printf ("2. Papel\n");
printf("3. tijeras\n");
do
scanf ("%d", &ju);
while ((ju == '\n') && (ju != '1'|| ju != '2' || ju != '3'));
while (getchar() != '\n');
ju --;
pc = rand () % 3;
printf("Ud. eligio %s, la PC eligio %s => ", v[ju], v[pc]);
if (ju == pc)
printf ("Hay empate\n");
else if ((ju == 0 && pc == 2) || (ju == 1 && pc == 0)|| (ju == 2 && pc == 1))
{
printf ("Ud. gana\n");
ganados++;
}
else
{
printf("La PC gana\n");
perdidos++;
}
jugados++;
printf("----------------------\n");
}
while (ganados < 5 && perdidos < 5);
if (ganados > perdidos)
printf ("Ud ha ganado en %d intentos.", jugados);
else
printf("Ud ha perdido en %d intentos.", jugados);
getchar();
return 0;
}