-8
#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

int main(){
int gnumber, rnumber;
    char choice;
    int tries;

    do {
    cout << "Welcome to the Number Guessing Game!" << endl;
    cout << endl; // breakline
    cout << "How many tries: ";
    cin >> tries;
    cout << endl;

    while (tries > 0){

    srand(time(NULL));
    rnumber = rand() % 99;

    cout <<"Enter an integer greater than or equal to 0 and less than 100:";
    cin >> gnumber;
    system("cls");

    if (tries != 1){
        if (gnumber < 100 && gnumber >= 0){

            if (gnumber == rnumber){
            cout << "Congratulations! You've guessed the number." << endl;
            tries--;
            cout << "Remaining tries: " << tries << endl;   
            }

            else if (gnumber > rnumber){
                cout << "Your guess is higher than the number." << endl;
                tries--;
                cout << " Guess Again!" << endl;
                cout << "Remaining tries: " << tries << endl;
            }
            else{
                cout << "Your guess is lower than the number." << endl;
                tries--;
                cout << " Guess Again!" << endl;
                cout << "Remaining tries: " << tries << endl;
            }
        }
        else
        cout << "Must greater or equal to 0 and lesser than 100!" << endl;
    }
    else 
    {
        cout << "Game over!" <<" The number is: " << rnumber << endl;
        cout << "Play Again? (Y/N)" << endl;
        cin >> choice;
        system("cls");

    }

    }
    }while(choice == 'Y' || choice == 'y'); //  

    system("pause");
    return 0;
    }

EVEN IF I ENTER CHOICE AS 'N' OR 'n' IT WONT STOP THE LOOP. And even if I enter 'Y' or 'y', it does not ask how many tries i wanted. Instead it just asks directly what integer I would want to enter. Please try to copy and compile the code to further understand what the problem is. Please help.

P.S.: This is a guessing program I'm making by the way...

  • Did you try stepping through your code, with a debugger? – Algirdas Preidžius Jan 24 '17 at 11:32
  • 1
    It's going to be a pretty nice trick for the shown code to do anything in response to "Y", "y", "N", or "n" when there's nothing in the shown code to do that. Your problem here is that your computer only does what you tell it to do, and not what you want it to do. – Sam Varshavchik Jan 24 '17 at 11:33
  • Actually it is 'Y' or 'y' not "Y" or "y" because choice is declared as char so 'Y' or 'y' is equal to letter Y or y. – cringyfudge420 Jan 24 '17 at 11:43

1 Answers1

0

the bug in your inner loop if the use enters a value other than one in else you don't decrement tries so it gets stuck:

    else 
    {
        cout << "Game over!" <<" The number is: " << rnumber << endl;
        cout << "Play Again? (Y/N)" << endl;
        cin >> choice;
        system("cls");
        tries--; // add this
    }
Raindrop7
  • 3,889
  • 3
  • 16
  • 27