Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     srand(time(NULL));
  9.    
  10.     int number = 0;
  11.     int tent = 0;
  12.     int guess = 0;
  13.     int nMax = 0;
  14.     int cont = 1;
  15.    
  16.     do {
  17.         cout<<"Qual è il numero massimo da indovinare? ";
  18.         cin>>nMax;
  19.        
  20.         if (nMax<=0) {
  21.             cout<<"Il numero non può essere minore di 1"<<endl;
  22.         }
  23.     } while (nMax<=0);
  24.    
  25.     number = rand()%(nMax+1);
  26.    
  27.     do {
  28.         cout<<"Quanti tentativi per indovinare? ";
  29.         cin>>tent;
  30.        
  31.         if (nMax<=0) {
  32.             cout<<"Il numero di tentativi non può essere minore di 1"<<endl;
  33.         }
  34.     } while (nMax<=0);
  35.    
  36.     do {
  37.         cout<<"Digita un numero tra 0 e "<<nMax<<": ";
  38.         cin>>guess;
  39.         if (guess<0 || guess>nMax) {
  40.             cout<<"Solo numeri tra 0 e "<<nMax<<endl;
  41.         } else if (guess==number) {
  42.             cout<<"Hai indovinato con "<<cont<<" tentativi"<<endl;
  43.         } else {
  44.             if (guess<number) {
  45.                 cout<<"Troppo piccolo, riprova"<<endl;
  46.             } else {
  47.                 cout<<"Troppo grande, riprova"<<endl;
  48.             }
  49.             cont++;
  50.         }
  51.         } while (cont<tent+1 && guess!=number);
  52.        
  53.         if (cont==tent+1) {
  54.             cout<<"\nNon hai indovinato, il numero era "<<number<<endl;
  55.         }
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement