Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <stdlib.h>
- using namespace std;
- int main() {
- srand(time(NULL));
- int number = 0;
- int tent = 0;
- int guess = 0;
- int nMax = 0;
- int cont = 1;
- do {
- cout<<"Qual è il numero massimo da indovinare? ";
- cin>>nMax;
- if (nMax<=0) {
- cout<<"Il numero non può essere minore di 1"<<endl;
- }
- } while (nMax<=0);
- number = rand()%(nMax+1);
- do {
- cout<<"Quanti tentativi per indovinare? ";
- cin>>tent;
- if (nMax<=0) {
- cout<<"Il numero di tentativi non può essere minore di 1"<<endl;
- }
- } while (nMax<=0);
- do {
- cout<<"Digita un numero tra 0 e "<<nMax<<": ";
- cin>>guess;
- if (guess<0 || guess>nMax) {
- cout<<"Solo numeri tra 0 e "<<nMax<<endl;
- } else if (guess==number) {
- cout<<"Hai indovinato con "<<cont<<" tentativi"<<endl;
- } else {
- if (guess<number) {
- cout<<"Troppo piccolo, riprova"<<endl;
- } else {
- cout<<"Troppo grande, riprova"<<endl;
- }
- cont++;
- }
- } while (cont<tent+1 && guess!=number);
- if (cont==tent+1) {
- cout<<"\nNon hai indovinato, il numero era "<<number<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement