Well, if you just want some general feedback, I'll just point out a few things.
First of all, please use [
code] [/code] tags for your code (it's the <> symbol under the "
Format:" options).
I would use
#include <cstdlib>
and
#include <ctime>
rather than
#include <stdlib.h>
and
#include <time.h>
(which are their C counterparts).
You also have a bunch of extraneous
#include
s. While that won't have any sort of negative effect on your program, I would just keep it simple and only
#include
what you really need.
I would also generally advice against using
system("pause")
and such. There's a nice article here explaining why:
http://www.cplusplus.com/forum/articles/11153/
Instead of typing
while(answer=='n' || answer=='N')
, you can make it simpler by just typing
while (tolower(answer) == 'n')
(you'll need to
#include <cctype>
in order to use
tolower).
Also,
1 2 3 4 5 6 7 8
|
if(again=='y' || again=='Y')
{
continue;
}
if(again=='n' || again=='N')
{
return 0;
}
|
can be simplified to just
1 2
|
if (tolower(again) == 'n')
return 0;
|
since you don't need that
continue
for the loop to run again.
Better yet, change the
while (true)
loop to a
do
-
while
loop, like this:
1 2 3 4
|
do
{
// The game
} while (again == 'y');
|
Also, you don't need the
while
loop here:
1 2 3 4 5 6 7
|
while(answer=='y' || answer=='Y')
{
system("cls");
cout<<"it took "<<tries<<" Tries to guess your number.\n\n";
system("pause");
break;
}
|
If the player enters 'y' or 'Y', then the program will exit the
while (answer == 'n' || answer == 'N')
loop, so you know the answer
has to be yes, so you don't need to check that again (much less enter a "loop" and
break
out of it after the first iteration).
By the way, you should really check to make sure that the player actually enters either yes or no, and not something weird like "hi" or "nnnnnnnnnnnnnnn" (which will answer "no" 15 times in a row).
And why not have the computer guess smarter choices (i.e. guess 50 first, then 25 or 75 depending on the player's answer, and so on)?
You should probably also say in your program that you want the player's number to be between 0 and 99. If I'm thinking of 200 (thinking that the game just wants me to think of any whole number), the computer will never guess it correctly....