C++ Guess the number game crashing with while function -


i made guess number game class assignment based on code c++ random number guessing game

i first tried make "goto" function , worked perfectly, teacher says need make using "while".

the problem program keeps closing after "troppo basso!" , "troppo alto!" messages appear, can tell me why?

#include <iostream> using namespace std;  int main() {     int nuovogioco = 0;     if (nuovogioco == 0)     {           srand(time(0));            int numero = rand() % 100 + 1;           int prova;           int variabile;           int periodo = 0;           nuovogioco++;           {                      while (periodo <1 )                      {                            cout << "a che numero sto pensando da 1 100? ";                            cout <<endl;                            cout << "se vuoi uscire digita e quando vuoi!";                            cout <<endl;                            cout << "inserisci un numero: ";                            periodo++;                      }                      while (periodo > 0)                      {                            cin >> prova;                            if (prova > numero)                             {                                      cout << "troppo alto!" <<endl;                                      periodo--;                            }                            if (prova < numero)                            {                                      cout << "troppo basso!" <<endl;                                      periodo--;                            }                            if (prova == numero)                            {                                       cout << "hai vinto! se vuoi rigiocare digita 1, altrimenti digita 2!";                                      cin >> variabile;                                      {                                          if  (variabile == 1)                                          {                                              variabile--;                                              nuovogioco--;                                          }                                          if  (variabile == 2)                                          {                                               cout << "byebye!  ";                                              system ("pause");                                          }                                      }                            }                      }           }     } } 

your program not crashing, exiting earlier intended. problem interplay between 2 loops:

while (periodo <1 ) {     /* output */     periodo++; } 

periodo starts off 0 , after first iteration has value of 1. loop terminates , pass next loop:

while (periodo > 0) {     cin >> prova;      if (prova > numero)      {         cout << "troppo alto!" <<endl;         periodo--;     }     if (prova < numero)     {         cout << "troppo basso!" <<endl;         periodo--;     }     if (prova == numero)     {          cout << "hai vinto! se vuoi rigiocare digita 1, altrimenti digita 2!";         cin >> variabile;         {             if  (variabile == 1)             {                 variabile--;                 nuovogioco--;             }             if  (variabile == 2)             {                  cout << "byebye!  ";                 system ("pause");             }         }     } } 

as periodo has value of 1 enters loop. decrement periodo , takes value 0.
loop terminates 0 not >0.

you need set periodo number of guesses want have (currently 1).
try initialising periodo number:

int periodo = 10; //set number of guesses 

and can remove first loop, doesn't @ moment. leave output:

cout << "a che numero sto pensando da 1 100? " << cout <<endl; cout << "se vuoi uscire digita e quando vuoi!"   << cout <<endl; cout << "inserisci un numero: "                  << cout <<endl; 

where loop is.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -