Skip to content

Instantly share code, notes, and snippets.

@divanibarbosa
Created March 25, 2022 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divanibarbosa/9de41262cf8e717e2c0e5f29b3d55c3d to your computer and use it in GitHub Desktop.
Save divanibarbosa/9de41262cf8e717e2c0e5f29b3d55c3d to your computer and use it in GitHub Desktop.
Jogo adivinha número em linguagem C++
// Criado por: profa. Divani Barbosa Gavinier
// Currículo Lattes: http://lattes.cnpq.br/8503400830635447
// divanibarbosa@gmail.com
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int GeraNum(int);
int AdivinhaNum();
main() {
cout << "Jogo do Adivinha\n";
cout << "Quantidade de chutes foi: "<< AdivinhaNum() << endl;
}
int GeraNum(int x) {
srand(time(NULL));
return rand()%(x+1);
}
int AdivinhaNum() {
int x, valor, chute, i=0;
cout << "Informe um valor para o intervalo [0,x] para que eu gere um valor aleatorio:\n";
cout << "Valor de x: ";
cin >> x;
valor = GeraNum(x);
cout << "Descubra o valor aleatorio gerado.\n";
do {
cout << "Responda qual eh o valor? ";
cin >> chute;
i++;
if(chute > valor) cout << "Muito alto. Tente novamente...\n";
else if(chute < valor) cout << "Muito baixo. Tente novamente...\n";
else {
cout << "ACERTOU!!\n";
break;
}
} while(1);
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment