computer guessing a number

Hey guys, this program my buddy and I created works, but we were wondering if there is a simpler way to do it or a way to improve the program. Here it is:

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <windows.h>
#include <cwchar>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main ()

{
int random;
char answer='n';
int tries=0;
char again;

srand(time(0));



while(true)
{
cout<<"hello. You are here to have the computer try to guess your number.\nThe number you are thinking of has to be a whole number.\n\n";
system("pause");
system("cls");


while(answer=='n' || answer=='N')
{

system("cls");
random= rand()% 100 + 0;
tries++;
cout<<"is your number "<<random <<" ? \t y= Yes/ n = No.\n";
cin>>answer;

system("cls");
}
while(answer=='y' || answer=='Y')
{

system("cls");
cout<<"it took "<<tries<<" Tries to guess your number.\n\n";
system("pause");
break;


}



system("cls");
cout<<"do you want to try again?\t y = Yes , n = No.\n";
cin>>again;

if(again=='y' || again=='Y')
{
continue;
}

if(again=='n' || again=='N')
{
return 0;
}

}
}

This program will have the user think of a number before the computer tries to guess it. Any feedback would help. Thanks
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....
Thanks for all the feedback! My friend did most of this program but I helped him with all of the mathematics.

not sure why he used the extra includes. lol

I will plug in the information you gave me and get back to you as soon as possible


EDIT
okay, I actually like the way the program is working now, but how will I be able to get an invalid answer response if they put in something other than "n" and where would I put it? here is the code I thought would work:
1
2
3
4
5
6
7
if(answer !='n' || answer !='y')
{
cin.get();
cout<<"Invalid answer!" <<endl;
cin.get();
break;
}
I have placed it just below
1
2
3
4
5
6
7
system("cls");
random= rand()% 100 + 0;
tries++;
cout<<"is your number "<<random <<" ? \t y= Yes/ n = No.\n";
cin>>answer;

system("cls");

and this results in breaking the program.

I have also placed it after here:
1
2
if (tolower(again) == 'n')
return 0;


this results in "y" being an invalid answer and it breaks it.

How can I fix this issue now?
Last edited on
Topic archived. No new replies allowed.