#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;
}
Dilshan_2
0
Newbie Poster
Recommended Answers
Jump to PostPlease 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 …
Jump to PostTest 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 …
All 6 Replies
Dilshan_2
0
Newbie Poster
Reverend Jim
4,780
Hi, I'm Jim, one of DaniWeb's moderators.
Moderator
Featured Poster
Dilshan_2
commented:
please refer below comment
+0
Dilshan_2
0
Newbie Poster
Dilshan_2
0
Newbie Poster
Dilshan_2
0
Newbie Poster
Reverend Jim
4,780
Hi, I'm Jim, one of DaniWeb's moderators.
Moderator
Featured Poster
Dilshan_2
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.