#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    srand(time(NULL));

    int tries = 0;
    int iNum;
    int UpperLim = 100;
    int LowerLim = 10;
    int iCompNum;

    cout << "Enter a number: ";
    cin >> iNum;

    do
    {
        iCompNum = rand() % (UpperLim - LowerLim) + LowerLim;

        cout << "\nComputer's guess(" << 10 - tries << " tries left): ";
        cout << iCompNum;
        ++tries;

        if (iNum > iCompNum)
        {
            cout << "\nThe number is less than the entered..!(" << 10 - tries << " tries left)\n\n";
            LowerLim = iCompNum+1;
        }
        else if (iNum < iCompNum)
        {
            cout << "\nThe number is greater than the entered..!(" << 10 - tries << " tries left)\n\n";
            UpperLim = iCompNum-1;
        }
        else if (iNum == iCompNum)
        {
            cout << "\nYou got my number!!!\n\nYou guessed the number in just " << tries << " tries!\n\n";
        }
        else if (tries >= 10)
         {
            cout << "You Lose!Correct Answer is:" << iNum << endl;
}
    } while (iNum != iCompNum);

    return 0;
}

Recommended Answers

All 6 Replies

For somewhat reason it is not properly working please be kind enough to do any alterations

Please be kind enough to describe the problem. If it won't compile then post the error and the line that generates it. If it errors out while running then do the same. If it is running but not doing what you want then describe it like "when I do X the program does Y but it should do Z". Get the picture?

commented: please refer below comment +0

Hi thank you for taking your time. So when the program runs out of tries it doesnt output the comment "You Lose!Correct Answer is:".

Problem is in the below code , but the error does not display but it get compiled

else if (iNum == iCompNum)
        {
            cout << "\nYou got my number!!!\n\nYou guessed the number in just " << tries << " tries!\n\n";
        }
        else if (tries >= 10)
         {
            cout << "You Lose!Correct Answer is:" << iNum << endl;
}
    } while (iNum != iCompNum);

    return 0;
}

error_c++.PNG

This post has no text-based content.

error_c++_2.PNG

This post has no text-based content.

Test for tries >= 10 at the top. As well, replace the literal 10 with a const like MAXTRIES and but a break after the "you lose" statement so you break out of the while loop.

Basically

do {
    if (++tries > MAXTRIES) {
        cout << "You Lose!Correct Answer is:" << iNum << endl;
        break;
    }

    if () {
        .
    } else if () {
        .
    } else if 

    etc.
commented: Thank You for the help +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.