Advertisement
Primitiv0

Guess The Number With Finite Tries

Sep 10th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <iostream> // cout, cin
  2. #include <stdlib.h>   // rand(), srand()
  3. #include <time.h>     // time()
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.   int n_gen = 0;
  10.   int n_ins = 0;
  11.   int intentos = 6;
  12.   bool adivinar;
  13.   adivinar = true;
  14.  
  15.   while (adivinar) {
  16.       adivinar = false;
  17.  
  18.          srand (time(NULL));
  19.        
  20.           n_gen = rand() % 100 + 1;
  21.          
  22.           do
  23.            {
  24.             cout << "Adivina el número del 1 al 100 (Intentos disponibles: "
  25.                  << intentos << "): ";
  26.             cin  >> n_ins;
  27.          
  28.             if(n_ins == n_gen)
  29.              {
  30.               cout << "Felicidades! has adivinado!!!" << endl;
  31.              }
  32.             else if(n_ins < n_gen)
  33.              {
  34.               cout << "El número es mayor" << endl;
  35.               intentos--;
  36.              }    
  37.             else if(n_ins > n_gen)
  38.              {
  39.               cout << "El número es menor" << endl;
  40.               intentos--;
  41.              }
  42.          
  43.             if(intentos == 0)
  44.              {
  45.               cout << "Lo siento, el número era: " << n_gen
  46.                    << ". Mejor suerte para la próxima!" << endl;
  47.              }
  48.          
  49.            }while(n_ins != n_gen && intentos > 0);
  50.          
  51.       cout << "Quieres jugar otra vez? s/n: ";
  52.      
  53.       char res;  // single character response. can validate input but i dont thinky ou need to
  54.       cin >> res;
  55.      
  56.       if (res == 's') {
  57.           adivinar = true;
  58.           intentos = 6;
  59.       }
  60.   }
  61.  
  62.  cout << "Se ha finalizado el juego. Hasta luego!";
  63.  
  64.  return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement