# Guess the computer number

Email
 Submitted on: 1/6/2015 12:55:00 AM By: NIHAR DINESH MANIYAR (from psc cd) Level: Beginner User Rating: By 4 Users Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++ Views: 765

This program is a puzzle where one needs to guess a number between 1 to 100 which is generated by computer. It shows how to use rand and srand functions

### INCLUDE files:

Can't Copy and Paste this?
 ```//************************************** //INCLUDE files for :Guess the computer number //************************************** iostream.h stdlib.h time.h```
code:
Can't Copy and Paste this?
 ``` //************************************** // Name: Guess the computer number // Description:This program is a puzzle where one needs to guess a number between 1 to 100 which is generated by computer. It shows how to use rand and srand functions // By: NIHAR DINESH MANIYAR (from psc cd) // // Inputs:The user has to guess and input a number between 1 to 100 which is generated by computer The user is given five chances // // Returns:This code returns whether the user has guessed the number successfully or not // // Assumes:The user should know a bit of programming in C/C++ //************************************** // This program is a puzzle where one needs to find a number between 1 to 100 which is generated by computer. //============================================================================================================// // This program helps us to know how to generate random numbers// //============================================================================================================// // If you want to compile this code in VC++ then name this as .cpp // If you want to compile this code in Unix then name this as .C (capital C) // Includes #include #include #include // Main Function int main() { // Prototypes void heading(); int nGuessedNum; // stores computer number int nInputNum; // stores inputted nubmer int nInd;// Index char nGotIt='n'; // Flag to indicate Success char nChoice='y'; // stores user choice // Loop while(nChoice=='y') { heading(); // Displays Heading nGotIt=' '; // Initailize a seed for random number generation using current time srand(static_cast(time(NULL))); // Increases randomness to get effective random numbers for(nInd=1;nInd<=10;nInd++) rand(); // Generate Computer number // formala : lower_range + (int) ( 1 + (lower_range + upper_range) * rand() / (RAND_MAX + 1.0)); // here 1.0 is used to get a double value, so that the random number varies nGuessedNum=1 + (int) (100 * rand()/(RAND_MAX + 1.0)); // This loop allows user to get the computer number in 5 chances for(nInd=1;nInd<6;nInd++) { cout<<"\n\n\t\tThis is Your Chance No = "<>nInputNum; // if your number is greater than computer's number if(nInputNum>nGuessedNum) cout<<"\n\t\tYour Number is Greater than Computer Number"; // if your number is lesser than computer's number else if(nInputNum>nChoice; } return 0; } // Heading which will displayed void heading() { cout<<"\t\tWelcome to the Game of Guessing...."; cout<<"\n\n\t\tThe computer has guessed a number between 1 to 100"; cout<<"\n\t\tYou have to the find the number. And you have 5 chances.."; cout<<"\n\t\tGet Set Go ..."; } ```

Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)