-2

I made this Guess the number game for a class assignment based on this code C++ Random number guessing game

I first tried to make it with the "goto" function and it worked perfectly, but my teacher says I need to make it using "while".

The problem is that the program keeps closing after the "Troppo basso!" and "Troppo alto!" messages appear, can somebody 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 a 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");
                                         }
                                     }
                           }
                     }
          }
    }
}
Community
  • 1
  • 1
Mgarlo
  • 15
  • 1

1 Answers1

4

Your program is not crashing, it is just exiting earlier than you intended. The problem is the interplay between the two loops:

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

Periodo starts off as 0 and after the first iteration has a value of 1. The loop terminates and you pass to the 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 a value of 1 it enters the loop. You then decrement Periodo and it takes the value 0.
The loop terminates as 0 is not >0.

you need to set Periodo to the number of guesses you want to have (currently only 1).
Try initialising Periodo to that number:

int Periodo = 10; //set to number of guesses

and you can remove the first loop, it doesn't actually do anything at the moment. Just leave your output:

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

where the loop currently is.

Baldrickk
  • 4,291
  • 1
  • 15
  • 27
  • I have to say, I'm also intrigued by the `if (Nuovogioco == 0){...}` as this will always be `true` as the previous line initialises it to `0`. – Baldrickk Sep 22 '14 at 13:27